Fragment懒加载基类

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.environment.protection.R;
import com.environment.protection.util.TLog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author csp
 * @data 2019/3/9.
 * Github https://github.com/CsurnameSP
 * @Description 懒加载基类
 */
public abstract class BaseLazyFragment extends Fragment {
    public Context mContext;
    public Activity mActivity;
    //碎片第一次生成时,onCreateView方法返回一个frameLayout,这个frameLayout内部不填充View,
    // 等到当前页面对用户可见时,再往frameLayout里面填充真正要显示的布局
    protected LayoutInflater mInflater;
    private View contentView;
    private ViewGroup mContainer;
    private FrameLayout frameLayout;


    private boolean isLoadSuccess = false;//懒加载布局是否完成
    private Unbinder unbinder;
    private static final String STATE_SAVE_IS_HIDDEN = "STATE_SAVE_IS_HIDDEN";

    /****使用懒加载时需要可能传递的参数***/
    public static final String INTENT_INT_INDEX = "index";
    public static final String STATUS = "Status";
    public static final String INTENT_BOOLEAN_LAZYLOAD = "intent_boolean_lazyLoad";
    private int mIndex = 0;
    private boolean mIsLazyLoad = true;
    private String mStates = "";


    protected abstract int getLayoutId();//懒加载时的布局

    protected abstract void afterLazyCreate();//懒加载布局完成后

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
        if (savedInstanceState != null) {
            boolean isSupportHidden = savedInstanceState.getBoolean(STATE_SAVE_IS_HIDDEN);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            if (isSupportHidden) {
                ft.hide(this);
            } else {
                ft.show(this);
            }
            ft.commit();
        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(STATE_SAVE_IS_HIDDEN, isHidden());
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        Bundle arguments = getArguments();
        if (arguments != null) {
            mIndex = arguments.getInt(INTENT_INT_INDEX, 0);
            mIsLazyLoad = arguments.getBoolean(INTENT_BOOLEAN_LAZYLOAD);
            mStates = arguments.getString(STATUS);
            TLog.i("执行setUserVisibleHint=" + isVisibleToUser + ",当前碎片=" + mIndex+",是否懒加载="+mIsLazyLoad+",当前状态="+mStates);
        }
        /***当前页面对用户可见时加载布局***/
        if (isVisibleToUser && !isLoadSuccess && contentView != null) {
            LoadLazyContentView();
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.mInflater = inflater;
        this.mContainer = container;
        frameLayout = new FrameLayout(mContext);
        if (mIsLazyLoad) {//默认使用懒加载
            if (getUserVisibleHint()) {
                //首次可见时直接加载布局
                LoadLazyContentView();
            } else {
                //首次不可见时加载个默认布局(空白)
                frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                contentView = inflater.inflate(R.layout.fragment_lazy_loading, container, false);
                frameLayout.addView(contentView);
            }
        }else {
            //不使用懒加载
            LoadLazyContentView();
        }
        return frameLayout;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    /**
     * 抽离一个生成替换内部View的方法
     */
    private void LoadLazyContentView() {
        isLoadSuccess = true;
        View view = mInflater.inflate(getLayoutId(), mContainer, false);
        unbinder = ButterKnife.bind(this, view);
        frameLayout.removeAllViews();
        frameLayout.addView(view);
        afterLazyCreate();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        this.mInflater = null;
        this.mContainer = null;
		this.contentView=null;
    }
}

 

你可能感兴趣的:(Fragment懒加载基类)