懒加载Fragment

场景:

  1. 第一次进入界面
  2. 左右滑动界面

setUserVisibleHint()被调用是在onCreateView()之前,基于这个特点:

  1. setUserVisibleHint()中初始化 isVisibleToUser = true,表示可见了,这时候调用prepareFetchData()并不会被执行,因为onActivityCreated()没初始化
  2. onActivityCreated()中初始化 isViewInitiated = true,表示生命周期被调用,这时候调用prepareFetchData()会执行。
  3. 实现懒加载的效果,需要数据只初始化一次,所以在 prepareFetchData() 再设置一个变量 isDataInitiated = true 表示数据已经初始化过。
  4. 这样页面切换后每次都会调用 setUserVisibleHint(),如果数据初始化过 isDataInitiated = true, 数据就不会被再次加载

总结:每次滑动界面都会调用setUserVisibleHint() ,在其中加载数据。但是执行这个方法需要确保 onActivityCreated() 初始化过,数据也没有加载过

public abstract class LazyLoadFragmen extends BaseFragment {

    protected boolean isViewInitiated;
    protected boolean isVisibleToUser;
    protected boolean isDataInitiated;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.e("Fragment生命周期", "onActivityCreated");
        isViewInitiated = true;
        prepareFetchData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.e("Fragment生命周期", "setUserVisibleHint");
        this.isVisibleToUser = isVisibleToUser;
        prepareFetchData();
    }

    public abstract void fetchData();

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

    public boolean prepareFetchData(boolean forceUpdate) {
        if (isVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
            fetchData();
            isDataInitiated = true;
            return true;
        }
        return false;
    }

}


/**
 * Created by zkt on 2018-8-17.
 * Description:
 */

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;

/**
 * 懒加载
 */
public abstract class LazyLoadFragment extends Fragment {
    //  标志位,标志已经初始化完成,因为setUserVisibleHint是在onCreateView之前调⽤的,

    //  在视图未初始化的时候,在lazyLoad当中就使⽤的话,就会有空指针的异常
    private boolean isFragmentPrepared;
    // 标志当前⻚⾯是否可⻅
    private boolean isVisible;
    // 标志数据是否加载过
    private boolean isDataPrepared;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        //懒加载
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isFragmentPrepared = true;
        lazyLoad();
    }

    protected void onVisible() {
        lazyLoad();
    }

    protected void onInvisible() {
    }

    //可见 + Fragment加载过 + 数据没加载过
    protected void lazyLoad() {
        if (isVisible && isFragmentPrepared && !isDataPrepared) {
            isDataPrepared = true;
            getData();
        }
    }

    /*  protected void lazyLoad() {
          if (!isVisible || !isPrepared) {
              return;
          }
          //数据请求
          getData();
      }
  */
    public abstract void getData();

}

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