不正确地工作viewPager

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了不正确地工作viewPager相关的知识,希望对你有一定的参考价值。

对不起,我的英语不好。我在我的应用程序中有BottomNavigationView,例如在Facebook和viewPager中使用滑动来扫描碎片,但是我在函数onPageSelected中遇到了一些问题,它总是将位置设置为4.这是MainActivity的代码:

private static final int NUM_PAGES = 5;
BottomNavigationView bottomNavigationView;
ViewPager viewPager;
PagerAdapter pagerAdapter;

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_tests:
                viewPager.setCurrentItem(0);
                return true;
            case R.id.navigation_courses:
                viewPager.setCurrentItem(1);
                return true;
            case R.id.navigation_profile:
                viewPager.setCurrentItem(2);
                return true;
            case R.id.navigation_leaderboard:
                viewPager.setCurrentItem(3);
                return true;
            case R.id.navigation_settings:
                viewPager.setCurrentItem(4);
                return true;
        }
        return false;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewPager = findViewById(R.id.pager);
    pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(pagerAdapter);

    bottomNavigationView = findViewById(R.id.navigation);
    bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
    viewPager.setCurrentItem(2);
    bottomNavigationView.getMenu().findItem(R.id.navigation_profile).setChecked(true);

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    setTitle("Tests");
                    bottomNavigationView.setSelectedItemId(R.id.navigation_tests);
                case 1:
                    setTitle("Courses");
                    bottomNavigationView.setSelectedItemId(R.id.navigation_courses);
                case 2:
                    setTitle("Profile");
                    bottomNavigationView.setSelectedItemId(R.id.navigation_profile);
                case 3:
                    setTitle("Leaderboard");
                    bottomNavigationView.setSelectedItemId(R.id.navigation_leaderboard);
                case 4:
                    setTitle("Settings");
                    bottomNavigationView.setSelectedItemId(R.id.navigation_settings);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
}

@Override
public void onBackPressed() {
    if (viewPager.getCurrentItem() == 2) {
        super.onBackPressed();
    } else if (viewPager.getCurrentItem() >= 3) {
        viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
    } else {
        viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
    }
}

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new FragmentTests();
            case 1:
                return new FragmentCourses();
            case 2:
                return new FragmentProfile();
            case 3:
                return new FragmentLeaderboard();
            case 4:
                return new FragmentSettings();
        }
        return null;
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}

}

这是应用程序的图像然后它再次将我移动到设置。当我点击配置文件

答案

您需要在ViewPager onPageSelected方法的break中编写switch case语句,如下所示

     @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        setTitle("Tests");
                        bottomNavigationView.setSelectedItemId(R.id.navigation_tests);
                        break;
                    case 1:
                        setTitle("Courses");
                        bottomNavigationView.setSelectedItemId(R.id.navigation_courses);
                        break;
                    case 2:
                        setTitle("Profile");
                        bottomNavigationView.setSelectedItemId(R.id.navigation_profile);
                        break;
                    case 3:
                        setTitle("Leaderboard");
                        bottomNavigationView.setSelectedItemId(R.id.navigation_leaderboard);
                        break;
                    case 4:
                        setTitle("Settings");
                        bottomNavigationView.setSelectedItemId(R.id.navigation_settings);
                        break;
                }
            }

以上是关于不正确地工作viewPager的主要内容,如果未能解决你的问题,请参考以下文章

标签菜单android - 不正确的调用片段

带有边距和页面转换器的片段内的 ViewPager 无法正确呈现

使用viewpager的片段事务返回空白视图

带有viewpager2 android的导航图

视频没有在片段 ViewPager 中暂停

在tablayout viewpager中运行调整选项卡片段