标签:
1:实例代码
private void showFragment(int index){ //开启事物 FragmentTransaction transaction = manager.beginTransaction(); hideFragment(transaction); switch (index) { case 0: if(fragment1 != null){//如过已经实例化,那么可直接显示出来 transaction.show(fragment1); }else{//如果没有实例化,先实例化,在添加 fragment1 = new TabFragment1(); transaction.add(R.id.content, fragment1); } break; case 1: if(fragment2 != null){ transaction.show(fragment2); }else{ fragment2 = new TabFragment2(); transaction.add(R.id.content, fragment2); } break; case 2: if(fragment3 != null){ transaction.show(fragment3); }else{ fragment3 = new TabFragment3(); transaction.add(R.id.content, fragment3); } break; default: break; } transaction.commit();//切记要提交事物 } /** * 隐藏所有的Fragment * @param ft */ private void hideFragment(FragmentTransaction ft){ if(fragment1 != null){ ft.hide(fragment1); } if(fragment2 != null){ ft.hide(fragment2); } if(fragment3 != null){ ft.hide(fragment3); } }
标签:
原文地址:http://www.cnblogs.com/wei1228565493/p/4774460.html