安卓Fragment的切换
程序员文章站
2022-05-05 14:22:56
...
// 初始化默认显示的界面
if (purchaserHomeFragment == null) {
purchaserHomeFragment = new PurchaserHomeFragment();
addFragment(purchaserHomeFragment);
showFragment(purchaserHomeFragment);
} else {
showFragment(purchaserHomeFragment);
}
// 购物车界面
if (purchaserCarFragment == null) {
purchaserCarFragment = new PurchaserCarFragment();
// 判断当前界面是否隐藏,如果隐藏就进行添加显示,false表示显示,true表示当前界面隐藏
if (!purchaserCarFragment.isHidden()) {
addFragment(purchaserCarFragment);
showFragment(purchaserCarFragment);
}
} else {
if (purchaserCarFragment.isHidden()) {
showFragment(purchaserCarFragment);
}
}
/** 添加fragment **/
public void addFragment(Fragment fragment) {
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
ft.add(R.id.fl_content, fragment);
ft.commit();
}
/** 显示fragment **/
public void showFragment(Fragment fragment) {
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
// 判断页面是否已经创建,如果已经创建,那么就隐藏掉
if (purchaserHomeFragment != null) {
ft.hide(purchaserHomeFragment);
}
if (purchaserTypeFragment != null) {
ft.hide(purchaserTypeFragment);
}
if (purchaserOrderFragment != null) {
ft.hide(purchaserOrderFragment);
}
if (purchaserCarFragment != null) {
ft.hide(purchaserCarFragment);
}
ft.show(fragment);
ft.commitAllowingStateLoss();
}
转载于:https://blog.51cto.com/luoguoxin/1734467
上一篇: 安卓activity切换动画