gpt4 book ai didi

java - Android 中的 fragment 重叠

转载 作者:行者123 更新时间:2023-12-02 08:52:25 24 4
gpt4 key购买 nike

我试图在 fragment 之间切换而不丢失以前的内容,所以我决定实现这种方式来做到这一点:

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();
}
}

使用此方法,我检查 fragment 是否已打开:如果是,我会显示它,如果没有,我会添加它。然后我将其声明为“alreadyOpened”。问题是,当我在 fragment 之间切换时,我看到所有三个 fragment 都重叠。我该如何解决?这是我调用 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();
}

最佳答案

您不应该在每次导航点击时创建新的 fragment 。在某处创建一次并将它们保存在List<Fragment> 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();

来自您的onNavigationItemSelected发送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();

}

关于java - Android 中的 fragment 重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60691539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com