• 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界面的时候,只需要调用那三个中的一个就可以了。

  • 相关阅读:
    SQL-42 删除emp_no重复的记录,只保留最小的id对应的记录
    数独代码(回溯算法。力扣37题)
    hmac_sha1 c++例子
    0-1背包问题简化。
    八皇后8皇后,探讨最效率的算法。
    System.NotSupportedException:“No data is available for encoding 1252. For information on defining a custom encoding
    TopK问题终极解答
    http与https区别,https是什么,s是指什么
    linux下 调试C#代码
    VS2017 远程调试linux(centos).net core程序(通过附加程序的方式)
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5105880.html
Copyright © 2020-2023  润新知