以下是我选择的底部导航视图项的代码
public static void setupBottomNavigationView(BottomNavigationViewEx bottomNavigationViewEx){
Log.d(TAG, "setupBottomNavigationView: Setting up NavigationView1");
bottomNavigationViewEx.enableAnimation(false);
bottomNavigationViewEx.enableItemShiftingMode(false);
bottomNavigationViewEx.enableShiftingMode(false);
bottomNavigationViewEx.setTextVisibility(false);
}
public static void enableNavigation(final Context context, BottomNavigationViewEx view){
view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()){
case R.id.nav_home:
fragment = new FragmentMarker();
loadFragment(fragment);
return true;
case R.id.nav_bookmark:
fragment = new FragmentBookmark();
loadFragment(fragment);
return true;
case R.id.nav_blog:
fragment = new FragmentBlog();
loadFragment(fragment);
return true;
case R.id.nav_notification:
fragment = new FragmentNotification();
loadFragment(fragment);
return true;
case R.id.nav_account:
fragment = new FragmentAccount();
loadFragment(fragment);
return true;
}
return false;
}
});
}
是否可以在不重新创建活动的情况下在底部导航栏上显示片段?
答案 0 :(得分:1)
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()) {
case R.id.nav_home:
fragment = new HomeFragment();
loadFragment(fragment);
return true;
case R.id.nav_bookmark:
fragment = new BookmarkFragment();
loadFragment(fragment);
return true;
case R.id.nav_blog:
fragment = new BlogFragment();
loadFragment(fragment);
return true;
}
return false;
}
};
private void loadFragment(Fragment fragment) {
// load fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}