Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android常用工具類封裝---Fragment

Android常用工具類封裝---Fragment

編輯:關於Android編程

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


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved