抱歉我的英语不好。我在我的应用程序中有BottomNavigationView,例如在Facebook和viewPager中使用swipe来剪切片段但是我在函数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;
}
}
}
这是应用的形象 然后它再次将我移动到设置。当我点击个人资料时
答案 0 :(得分:0)
您需要在ViewPager的break
onPageSelected方法中编写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;
}
}