Android Fragment懒加载

Android Fragment懒加载

BaseLazyFragment

/**
 * Fragment 懒加载 + MVP
 *
 * @author machangbao
 * @date 2019年03月08日
 */
public abstract class BaseMvpLazyFragment<P extends BaseContract.IPresenter> extends BaseMvpFragment<P> {

    protected boolean isActivityCreated;    //Activity是否已创建
    protected boolean isVisibleToUser;      //Fragment是否对用户可见
    protected boolean isDataInitiated;      //Fragment是否已加载过数据

    /**
     * After
     * <pre>
     *     @Override
     *     public void onActivityCreated(Bundle savedInstanceState) {
     *         super.onActivityCreated(savedInstanceState);
     *          initData() ;
     *     }
     * </pre>
     * {@link com.ando.base.base.BaseFragment#onActivityCreated}
     */
    @Override
    protected void initData() {
        isActivityCreated = true;
        prepareFetchData();
    }

    /**
     * @deprecated Use {@link FragmentTransaction#setMaxLifecycle}
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        prepareFetchData();
    }


    public boolean prepareFetchData() {
        return prepareFetchData(false);
    }

    public boolean prepareFetchData(boolean forceUpdate) {
        if (isActivityCreated && isVisibleToUser && (!isDataInitiated || forceUpdate)) {
            /*
             * 在该方法中初始化 Fragment 的数据
             * <p>
             * initLayout -> initViews -> onActivityCreated -> initData -> initLazyData
             */
            initLazyData();
            isDataInitiated = true;
            return true;
        }
        return false;
    }

    protected abstract void initLazyData();

    public void setDataInitiated() {
        this.isDataInitiated = true;
    }

    /*******************************todo 多种空视图处理方式待优化**********************************/
}

BaseMvpFragment

public abstract class BaseMvpFragment<P extends BaseContract.IPresenter> extends BaseFragment implements BaseContract.IView {

    protected P mPresenter;

    @Override
    protected void initMvp() {
        super.initMvp();
        initPresenter();
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
    }

    protected abstract void initPresenter();

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter = null;
        }
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void complete() {

    }
}

BaseContract

public interface BaseContract {

    interface IView {

        /**
         * 请求错误
         */
        void showError(String msg);

        void complete();
    }

    interface IPresenter<T> {
        /**
         * 绑定
         *
         * @param view view
         */
        void attachView(T view);

        /**
         * 解绑
         */
        void detachView();
    }
}
posted @ 2022-10-13 07:08  javakam  阅读(33)  评论(0编辑  收藏  举报  来源