RadioGroup+Fragment 使用Fragment的add()方法,防止使用replace每次都重新加载页面,造成资源浪费

时间:2022-02-26 15:07:08

radiogroup+fragment是很常用的主页导航控件,之前为了代码简便一直使用的replace()替换fragment,代码如下:

getSupportFragmentManager().beginTransaction().replace(R.id.framlayout,fragment).commit();
这种方法使用起来方便,但最大的问题是,每次执行都会把之前的fragment队列中的所有fragment对象全部清除,然后重新加载一个新的fragment进去,
相当于使用remove()+ add();
这会造成每次页面切换的时候都重新加载页面,如果你里面有网络请求等耗时操作,就更浪费资源。
所以研究了一下,使用add() + show() + hide()三个方法结合使用。
使用这三个方法你会发现 当第二次切换时,oncreateview()方法就不再执行。核心代码如下:
  fragment = manager.findFragmentByTag("first");
if (fragment == null) {
fragment = new HomeFragment();
LogUtils.logInfoStar("new了一次");
transaction.add(R.id.framlayout, fragment, "first").commit();
} else if(fragment.isAdded()){
transaction.show(fragment).commit();
LogUtils.logInfoStar("zhixingzzh");
}else{
transaction.add(R.id.framlayout, fragment, "first").commit();
}
if (preFragment != null)
transaction.hide(preFragment); //隐藏上一个fragment
preFragment = fragment;//记录上一个fragment

全部代码如下:

 radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment fragment = null;
switch (checkedId) {
case R.id.radio_first: fragment = manager.findFragmentByTag("first");
if (fragment == null) {
fragment = new HomeFragment();
LogUtils.logInfoStar("new了一次");
transaction.add(R.id.framlayout, fragment, "first").commit();
} else if(fragment.isAdded()){
transaction.show(fragment).commit();
LogUtils.logInfoStar("zhixingzzh");
}else{
transaction.add(R.id.framlayout, fragment, "first").commit();
} break;
case R.id.radio_second:
fragment=manager.findFragmentByTag("second");
if(fragment==null){
fragment = new CategoryFragment();
transaction.add(R.id.framlayout,fragment,"second").commit();
}else if(fragment.isAdded()){
transaction.show(fragment).commit();
}else{
transaction.add(R.id.framlayout,fragment,"second").commit(); }
break;
case R.id.radio_third:
fragment=manager.findFragmentByTag("third");
if(fragment==null){
fragment = new SearchFragment();
transaction.add(R.id.framlayout,fragment,"third").commit();
}else if(fragment.isAdded()){
transaction.show(fragment).commit();
}else{
transaction.add(R.id.framlayout,fragment,"third").commit();
}
break;
case R.id.radio_fourth: fragment=manager.findFragmentByTag("fourth");
if(fragment==null){
fragment = new ShoppingCartFragment();
transaction.add(R.id.framlayout,fragment,"fourth").commit();
}else if(fragment.isAdded()){
transaction.show(fragment).commit();
}else{
transaction.add(R.id.framlayout,fragment,"fourth").commit();
}
break;
case R.id.radio_fifth:
fragment=manager.findFragmentByTag("fifth");
if(fragment==null){
fragment = new MoreFragment();
transaction.add(R.id.framlayout,fragment,"fifth").commit();
}else if(fragment.isAdded()){
transaction.show(fragment).commit();
}else{
transaction.add(R.id.framlayout,fragment,"fifth").commit();
}
break;
}
if (preFragment != null)
transaction.hide(preFragment);
preFragment = fragment;
}
});
//使用这种方式设置默认选中,监听方法会执行两次,也就是第一的首页fragment会被创建两次。
//radiogroup.check(R.id.radio_first);
//使用这种方式设置默认选中 页面改变监听方法只会执行一次
radioFirst.setChecked(true);

关于默认选择首页的方式导致初始化页面时执行两次监听方法的详细讲解可参考下面博文,简单易懂:

http://blog.csdn.net/piglovesula/article/details/9820521
以上方法自己摸索,一股骚西西的菜鸟味,集成环信SDK时发现了一种很牛逼简便的写法,如下:
/**
* button点击事件
*
* @param view
*/
public void onTabClicked(View view) {
switch (view.getId()) {
case R.id.btn_conversation:
index = 0;
break;
case R.id.btn_address_list:
index = 1;
break;
case R.id.btn_setting:
index = 2;
break;
}
if (currentTabIndex != index) {
FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
trx.hide(fragments[currentTabIndex]);
if (!fragments[index].isAdded()) {
trx.add(R.id.fragment_container, fragments[index]);
}
trx.show(fragments[index]).commit();
}
mTabs[currentTabIndex].setSelected(false);
// 把当前tab设为选中状态
mTabs[index].setSelected(true);
currentTabIndex = index;
}