首页 > 代码库 > Fragment与FragmentActivity通信封装

Fragment与FragmentActivity通信封装

在项目里面会经常用到Fragment与FragmentActivity,比如Fragment之间的界面切换与Fragment之间的值传递等等,为了方便起见我把Fragment和FragmentActivity进一步封装,那在实际开发中只需要继承父类,然后调用一个方法就可以实现Fragment之间界面切换和值传递

1、FragmentActivity类的封装

<span style="font-size:14px;">public class BaseChangeFragments extends BaseActivity {

	protected FragmentManager mFragmentManager;
	protected FragmentTransaction mFragmentTransaction;

	protected String mCurrentFragmentTag;
	protected Map<String, BaseFragment> mapFragments = new HashMap<String, BaseFragment>();

	@Override
	protected void beforeCreate() {
		super.beforeCreate();
		mFragmentManager = getSupportFragmentManager();
	}

	protected FragmentTransaction ensureTransaction() {
		if (mFragmentTransaction == null) {
			mFragmentTransaction = mFragmentManager.beginTransaction();
			// mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
			// mFragmentTransaction.addToBackStack(null);
			mFragmentTransaction
					.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		}

		return mFragmentTransaction;
	}

	protected BaseFragment getFragment(String tag) {
		BaseFragment f = (BaseFragment) (mFragmentManager
				.findFragmentByTag(tag));
		if (f == null) {
			// 在这里判断tag,不同则实例化对应的fragment
			f = mapFragments.get(tag);
		}
		return f;
	}

	protected void attachFragment(int layout, Fragment f, String tag) {
		if (f != null) {
			if (f.isDetached()) {
				ensureTransaction();
				mFragmentTransaction.attach(f);
			} else if (!f.isAdded()) {
				ensureTransaction();
				mFragmentTransaction.add(layout, f, tag);
			}
		}
	}

	protected void detachFragment(Fragment f) {
		if (f != null && !f.isDetached()) {
			ensureTransaction();
			mFragmentTransaction.detach(f);
		}
	}

	/**
	 * @param layout
	 * @param f
	 * @param tag
	 */
	protected void showFragment(int layout, BaseFragment f, String tag) {
		if (f != null) {
			if (!f.isAdded()) {
				ensureTransaction();
				f.updateNet();
				mFragmentTransaction.add(layout, f, tag);
			} else {
				ensureTransaction();
				f.updateNet();
				mFragmentTransaction.show(f);
			}
		}
	}

	/**
	 * 进行传值
	 * 
	 * @param layout
	 * @param f
	 * @param tag
	 * @param bundle
	 */
	protected void showFragment(int layout, BaseFragment f, String tag,
			Bundle bundle) {
		if (f != null) {
			if (!f.isAdded()) {
				ensureTransaction();
				f.updateNet(bundle);
				mFragmentTransaction.add(layout, f, tag);
			} else {
				ensureTransaction();
				f.updateNet(bundle);
				mFragmentTransaction.show(f);
			}
		}
	}

	protected void hideFragment(Fragment f) {
		if (f != null) {
			if (f.isAdded()) {
				ensureTransaction();
				mFragmentTransaction.hide(f);
			}
		}
	}

	protected void commitTransactions() {
		if (mFragmentTransaction != null && !mFragmentTransaction.isEmpty()) {
			mFragmentTransaction.commit();
			mFragmentTransaction = null;
		}
	}

	/**
	 * 采用attach和detach来实现fragment的切换,每一次都会进入onCreateView
	 * 
	 * @param tag
	 */
	protected void switchFragmenCreate(String tag) {
		if (TextUtils.equals(mCurrentFragmentTag, tag))
			return;
		if (mCurrentFragmentTag != null)
			detachFragment(getFragment(mCurrentFragmentTag));
		attachFragment(R.id.container, getFragment(tag), tag);
		mCurrentFragmentTag = tag;
		commitTransactions();
	}

	/**
	 * 采用show和hide来实现fragment的切换,只有第一次都会进入onCreateView
	 * 
	 * @param tag
	 */
	protected void switchFragmen(String tag) {
		if (TextUtils.equals(mCurrentFragmentTag, tag))
			return;
		if (mCurrentFragmentTag != null)
			hideFragment(getFragment(mCurrentFragmentTag));
		showFragment(R.id.container, getFragment(tag), tag);
		mCurrentFragmentTag = tag;
		commitTransactions();
	}
	
	/**
	 * 切换界面,通过bundle来传值
	 * @param tag
	 * @param bundle
	 */
	protected void switchFragmen(String tag, Bundle bundle) {
		if (TextUtils.equals(mCurrentFragmentTag, tag))
			return;
		if (mCurrentFragmentTag != null)
			hideFragment(getFragment(mCurrentFragmentTag));
		showFragment(R.id.container, getFragment(tag), tag, bundle);
		mCurrentFragmentTag = tag;
		commitTransactions();
	}

}</span>
上面包含了三种切换方式分别是:(1)attach和detach (2)show和hide -- 不传值(3)show和hide--传值

其中传值方式采用接口回调的方式实现,比如下面这个方法:

<span style="font-size:14px;">/**
	 * 进行传值
	 * 
	 * @param layout
	 * @param f
	 * @param tag
	 * @param bundle
	 */
	protected void showFragment(int layout, BaseFragment f, String tag,
			Bundle bundle) {
		if (f != null) {
			if (!f.isAdded()) {
				ensureTransaction();
				f.updateNet(bundle);  --采用接口回调告知fragment显示以及更新数据两个功能
				mFragmentTransaction.add(layout, f, tag);
			} else {
				ensureTransaction();
				f.updateNet(bundle);
				mFragmentTransaction.show(f);
			}
		}
	}</span>

其中里面BaseActivity这个类是在我的项目的基础类,我把onCreate里面的添加的空方法写出来

<span style="font-size:14px;">@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		startIntent = new Intent();
		beforeCreate();
		LogUtils.customTagPrefix = "IDPoc";
		initView();
		initListener();
		initData();
	}
</span>

回调接口如下:

<span style="font-size:14px;">public interface IFragmentChange {
	void setId(String id, Bundle bundle);
}</span>

在Fragment类中实现:

<span style="font-size:14px;"></span><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="font-size:14px;"><span style="color: #bb2ca2">private</span> IFragmentChange mFragmentChange;</span></p><span style="font-size:14px;">@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mFragmentChange = (IFragmentChange) activity;
	}

	@Override
	public void onDetach() {
		super.onDetach();
		mFragmentChange = null;
	}
</span>

这样的话,切换fragment界面的时候,只需要调用那三个中的一个就可以了。