Android常用工具类封装---Fragment
Fragment切换 /** ** Fragment切换 @Params toFragment 将要切换到的Fragment resId 装载Fragment的view Id index Fragment的标识index toleft 判断Fragment向左切换还是向右切换,以采用不同的动画 Notes: R.anim.push_left_in等均为简单的Tranlate动画 mCurrentFragment为当前所在的Fragment,继承自BaseFragment */ protected void switchDiffFragmentContent (Fragment toFragment, int resId, int index, boolean toleft){ if (null == mCurrentFragment || null == toFragment){ return; } if (mCurrentFragment.getArguments().getInt("Index") != toFragment.getArguments.getInt("index")) { FragmentTransaction fragmentTransaction; fragmentTrasaction = getChildFragmentManager().beginTransaction(); if (toleft) { fragmentTrasaction.SetCustomAnimations(R.anim.push_left_in, R.anim.push_left_out); }else{ fragmentTrasaction.setCustomAnimations(R.anim.push_right_in, R.anim.push_right_out); } //先判断是否添加过 if (!toFragment.isAdded()) { //隐藏当前fragment,add下一个fragment fragmentTrasaction.hide(mCurrentFragment); fragmentTrasaction.add(resId, toFragment, String.valueOf(index)); fragmentTransactoin,commit(); }else{ //隐藏当前fragment,show下一个fragment fragmentTransaction.hide(mCurrentFragment); fragmentTransaction.show(toFragment); fragmentTransaction.commit(); } mCurrentFragment = (BaseFragment)toFragment; } }
来自:http://blog.csdn.net/lanximu/article/details/41414933