radiogroup+fragment是很常用的主页导航控件,之前为了代码简便一直使用的replace()替换fragment,代码如下:
getSupportFragmentManager().beginTransaction().replace(R.id.framlayout,fragment).commit();
这种方法使用起来方便,但最大的问题是,每次执行都会把之前的fragment队列中的所有fragment对象全部清除,然后重新加载一个新的fragment进去,
相当于使用remove()+ add();
这会造成每次页面切换的时候都重新加载页面,如果你里面有网络请求等耗时操作,就更浪费资源。
所以研究了一下,使用add() + show() + hide()三个方法结合使用。
使用这三个方法你会发现 当第二次切换时,oncreateview()方法就不再执行。核心代码如下:
1 fragment = manager.findFragmentByTag("first"); 2 if (fragment == null) { 3 fragment = new HomeFragment(); 4 LogUtils.logInfoStar("new了一次"); 5 transaction.add(R.id.framlayout, fragment, "first").commit(); 6 } else if(fragment.isAdded()){ 7 transaction.show(fragment).commit(); 8 LogUtils.logInfoStar("zhixingzzh"); 9 }else{ 10 transaction.add(R.id.framlayout, fragment, "first").commit(); 11 }
if (preFragment != null)
transaction.hide(preFragment); //隐藏上一个fragment
preFragment = fragment;//记录上一个fragment
全部代码如下:
1 radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 2 @Override 3 public void onCheckedChanged(RadioGroup group, int checkedId) { 4 FragmentManager manager = getSupportFragmentManager(); 5 FragmentTransaction transaction = manager.beginTransaction(); 6 Fragment fragment = null; 7 switch (checkedId) { 8 case R.id.radio_first: 9 10 fragment = manager.findFragmentByTag("first"); 11 if (fragment == null) { 12 fragment = new HomeFragment(); 13 LogUtils.logInfoStar("new了一次"); 14 transaction.add(R.id.framlayout, fragment, "first").commit(); 15 } else if(fragment.isAdded()){ 16 transaction.show(fragment).commit(); 17 LogUtils.logInfoStar("zhixingzzh"); 18 }else{ 19 transaction.add(R.id.framlayout, fragment, "first").commit(); 20 } 21 22 break; 23 case R.id.radio_second: 24 fragment=manager.findFragmentByTag("second"); 25 if(fragment==null){ 26 fragment = new CategoryFragment(); 27 transaction.add(R.id.framlayout,fragment,"second").commit(); 28 }else if(fragment.isAdded()){ 29 transaction.show(fragment).commit(); 30 }else{ 31 transaction.add(R.id.framlayout,fragment,"second").commit(); 32 33 } 34 break; 35 case R.id.radio_third: 36 fragment=manager.findFragmentByTag("third"); 37 if(fragment==null){ 38 fragment = new SearchFragment(); 39 transaction.add(R.id.framlayout,fragment,"third").commit(); 40 }else if(fragment.isAdded()){ 41 transaction.show(fragment).commit(); 42 }else{ 43 transaction.add(R.id.framlayout,fragment,"third").commit(); 44 } 45 break; 46 case R.id.radio_fourth: 47 48 fragment=manager.findFragmentByTag("fourth"); 49 if(fragment==null){ 50 fragment = new ShoppingCartFragment(); 51 transaction.add(R.id.framlayout,fragment,"fourth").commit(); 52 }else if(fragment.isAdded()){ 53 transaction.show(fragment).commit(); 54 }else{ 55 transaction.add(R.id.framlayout,fragment,"fourth").commit(); 56 } 57 break; 58 case R.id.radio_fifth: 59 fragment=manager.findFragmentByTag("fifth"); 60 if(fragment==null){ 61 fragment = new MoreFragment(); 62 transaction.add(R.id.framlayout,fragment,"fifth").commit(); 63 }else if(fragment.isAdded()){ 64 transaction.show(fragment).commit(); 65 }else{ 66 transaction.add(R.id.framlayout,fragment,"fifth").commit(); 67 } 68 break; 69 } 70 if (preFragment != null) 71 transaction.hide(preFragment); 72 preFragment = fragment; 73 } 74 }); 75 //使用这种方式设置默认选中,监听方法会执行两次,也就是第一的首页fragment会被创建两次。 76 //radiogroup.check(R.id.radio_first); 77 //使用这种方式设置默认选中 页面改变监听方法只会执行一次 78 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; }