Android 中重叠的片段
Fragments overlapping in Android
我想在片段之间切换而不丢失以前的内容,所以我决定以这种方式实现:
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();
}
您不应在每次导航点击时都创建新的片段。在某处创建一次并将它们保存在 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();
}
我想在片段之间切换而不丢失以前的内容,所以我决定以这种方式实现:
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();
}
您不应在每次导航点击时都创建新的片段。在某处创建一次并将它们保存在 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();
}