当我点击bottomnavigation时,片段不会被替换

时间:2017-07-03 15:17:43

标签: android bottomnavigationview

为什么当我点击项目导航视图片段而不是替换内容时。

navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
  @Override
  public void onNavigationItemReselected(@NonNull MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.navigation_home) {
      fragment = new HomeFragment();
    } else if (id == R.id.navigation_dashboard) {
      fragment = new PopularFragment();
    } else if (id == R.id.navigation_notifications) {
      fragment = new FeaturedFragment();
    }
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.content, fragment);
    transaction.commit();
  }

});

}

1 个答案:

答案 0 :(得分:0)

在android.support.v4中使用以下代码

FragmentTransaction ft = getFragmentManager().beginTransaction();
DemoFragment w1 = new DemoFragment();
ft.addToBackStack("");
ft.replace(R.id.listFragment, w1);
ft.commit();