我试图在片段之间切换而又不丢失先前的内容,所以我决定实现这种方式:
private void loadFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().hide(fragment);
if (!alreadyOpened[currentIndex]) {
alreadyOpened[currentIndex] = true;
fragmentManager.beginTransaction().add(R.id.fragment_container,fragment).commit();
} else {
fragmentManager.beginTransaction().show(fragment).commit();
}
}
使用这种方法,我检查片段是否已经打开:如果是,则显示它,如果没有,则添加它。然后,我将其声明为“ alreadyOpened”。问题是,当我在片段之间切换时,我看到所有三个片段重叠。我该如何解决?当我调用loadFragment方法时,这是菜单栏:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.bbn_item1:
currentIndex = 0;
loadFragment(new HomeFragment());
return true;
case R.id.bbn_item2:
currentIndex = 1;
loadFragment(new PopularFragment());
return true;
case R.id.bbn_item3:
currentIndex = 2;
loadFragment(new PlayingFragment());
return true;
}
return false;
}
};
编辑:新代码
List<Fragment> fragments = new ArrayList<>();
private void loadFragment(Fragment fragment) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (alreadyOpened[currentIndex]) {
ft.replace(R.id.fragment_container, fragments.get(currentIndex));
} else {
fragments.add(currentIndex, fragment);
alreadyOpened[currentIndex] = true;
ft.replace(R.id.fragment_container, fragment);
}
ft.addToBackStack(null);
ft.commit();
}
问题来源:Stack Overflow
您不应在每次导航单击时都创建新的片段。在某个地方创建一次并将其保存在List fragments数组中。在那之后使用fragmentManager.beginTransaction().replace(R.id.fragment_layout, fragment);这里是一个小例子:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left); //optional
ft.replace(R.id.fragment_layout, fragments.get(selectedFragment));
ft.addToBackStack(null);
ft.commit();
从您的onNavigationItemSelectedsend selectedFragment变量中,匹配fragments列表中的位置。
编辑:怎么样:
public HomeFragment homeFragment;
public PopularFragment popularFragment;
public PlayingFragment playingFragment;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.bbn_item1:
if(homeFragment == null){ homeFragment =new HomeFragment();}
loadFragment(homeFragment )
return true;
case R.id.bbn_item2:
if(homeFragment == null){ popularFragment =new PopularFragment();}
loadFragment(popularFragment)
return true;
case R.id.bbn_item3:
if(homeFragment == null){ playingFragment =new PlayingFragment();}
loadFragment(playingFragment);
return true;
}
return false;
}
public void loadFragment(Fragment fragment){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left); //optional
ft.replace(R.id.fragment_layout, fragment);
ft.addToBackStack(null);
ft.commit();
}
回答来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。