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;
	}
 }


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。