我在主要活动中设置了3个片段,并希望使用BottomNavigationView对其进行导航。我想添加一次,然后在它们之间进行切换而不会破坏片段。一切正常,除了添加到SupportFragmentManager的第一个片段始终消失(尝试更改顺序,因此问题不在于Fragments本身)。实际上,它并没有消失,但是显示了占用容器的最后一个Fragment。 例如。我转到位置3,片段3出现在容器中,然后单击位置1,片段3仍将占据容器。但是,如果我点击位置2,将会出现片段2。我如何添加片段:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, fragment1); //whatever gets added here ends up being invisible
ft.commitAllowingStateLoss();
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, fragment2);
ft.commitAllowingStateLoss();
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, fragment3);
ft.commitAllowingStateLoss();
我如何使用OnNavigationItemSelectedListener在它们之间进行切换:
private BottomNavigationView.OnNavigationItemSelectedListener navigation_listener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
Fragment previousFragment = getSupportFragmentManager().findFragmentById(R.id.container);
switch(item.getItemId()){
case R.id.bottom_navigation_bar_position1:
selectedFragment = fragment1;
break;
case R.id.bottom_navigation_bar_position2:
selectedFragment = fragment2;
break;
case R.id.bottom_navigation_bar_position3:
selectedFragment = fragment3;
break;
}
getSupportFragmentManager().beginTransaction().hide(previousFragment).commit();
getSupportFragmentManager().beginTransaction().show(selectedFragment).commit();
return true;
}
};
XML菜单导航:
<item
android:id="@+id/bottom_navigation_bar_position1"
android:title="Title1"
android:icon="@drawable/ic1"
/>
<item
android:id="@+id/bottom_navigation_bar_position2"
android:title="Title2"
android:icon="@drawable/ic2"
/>
<item
android:id="@+id/bottom_navigation_bar_position3"
android:title="Title3"
android:icon="@drawable/ic3"
/>
顺便说一句。只是使用replace()而不是显示和隐藏作品,但这不是目标... 我还测试了第一个添加的Fragment是否退出(并且不会被销毁)并且确实存在... 谢谢!
答案 0 :(得分:1)
我没有尝试显示和隐藏片段,而是通过使用ViewPager和SectionsPageAdapter管理片段来解决该问题。链接这两者后,请使用ViewPager.setOffscreenPageLimit(3)确保没有碎片被销毁。我的BottomNavigationView.OnNavigationItemSelectedListener现在看起来像这样:
private BottomNavigationView.OnNavigationItemSelectedListener navigation_listener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.bottom_navigation_bar_position1:
tab_view_pager.setCurrentItem(0);
return true;
case R.id.bottom_navigation_bar_position2:
tab_view_pager.setCurrentItem(1);
return true;
case R.id.bottom_navigation_bar_position3:
tab_view_pager.setCurrentItem(2);
return true;
}
return false;
}
};
答案 1 :(得分:0)
您必须设置为BottomNavigationView.OnNavigationItemSelectedListener
选择的第0个位置项目。找到Menu
bottom_navigation_bar_position1
我在代码中使用了此代码,请根据您的需求进行重构
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this);
Menu menu = navigation.getMenu();
this.onNavigationItemSelected(menu.findItem(R.id.navigation_doc));