Fragment懒加载:LazyFragment

/**
 * @Author: 淘跑
 * @Date: 2018/7/23 10:41
 * @Use:
 */
public class LazyFragment extends Fragment {

    private int mType;
    /**
     * Fragment是否可见状态
     */
    public boolean isFragmentVisible;
    /**
     * 标志位,View是否已经初始化完成。
     */
    private boolean isPrepared;
    /**
     * 是否第一次加载
     */
    private boolean isFirstLoad = true;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mType = getArguments().getInt("type");


        Log.i("----------------", mType + "onCreate");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_lazy, container, false);

        // 若 viewpager 不设置 setOffscreenPageLimit 或设置数量不够
        // 销毁的Fragment onCreateView 每次都会执行(但实体类没有从内存销毁)
        isFirstLoad = true;


        return inflate;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i("----------------", mType + "onViewCreated");
        //界面初始化完成
        isPrepared = true;

        loadData();
    }

    /**
     * 如果是与ViewPager一起使用,调用的是setUserVisibleHint
     *
     * @param isVisibleToUser 是否显示出来了
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            onVisible();
        } else {
            onInvisible();
        }
    }

    /**
     * 如果是通过FragmentTransaction的show和hide的方法来控制显示,调用的是onHiddenChanged.
     * 若是初始就show的Fragment 为了触发该事件 需要先hide再show
     *
     * @param hidden hidden True if the fragment is now hidden, false if it is not
     *               visible.
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            onVisible();
        } else {
            onInvisible();
        }
    }

    protected void onInvisible() {
        isFragmentVisible = false;
        Log.i("----------------", mType + "onInvisible");

    }

    /**
     * 当界面可见的时候执行
     */
    protected void onVisible() {
        Log.i("----------------", mType + "onVisible");
        isFragmentVisible = true;
        loadData();

    }

    /**
     * 这里执行懒加载的逻辑
     */
    protected void lazyLoad() {
        Log.i("----------------", mType + "                    lazyLoad");
    }

    private void loadData() {
        //判断View是否已经初始化完成 并且 fragment是否可见 并且是第一次加载
        if (isPrepared && isFragmentVisible && isFirstLoad) {
            isFirstLoad = false;
            lazyLoad();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isPrepared = false;
        Log.i("----------------", mType + "onDestroy");
    }

    /**
     * Fragment 传值
     *
     * @return
     */
    public static LazyFragment getInstance(int type) {
        LazyFragment fragment = new LazyFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

}

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