如何把activity封装成fragment

 我来答
匿名用户
2016-05-02
展开全部
可以把Fragment和FragmentActivity进一步封装,那在实际开发中只需要继承父类,然后调用一个方法就可以实现Fragment之间界面切换和值传递
1、FragmentActivity类的封装
[java] view plain copy print?
<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--传值
其中传值方式采用接口回调的方式实现,比如下面这个方法:
[java] view plain copy print?
<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里面的添加的空方法写出来
[java] view plain copy print?
<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>
回调接口如下:
[java] view plain copy print?
<span style="font-size:14px;">public interface IFragmentChange {
void setId(String id, Bundle bundle);
}</span>

在Fragment类中实现:
[java] view plain copy print?
<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界面的时候,只需要调用那三个中的一个就可以了。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式