Fragment+ViewPager实现真正的懒加载

我们通常使用Fragment+ViewPager实现页面的切换,在ViewPager的默认机制中,我们能设置预加载个数

vpContent.setOffscreenPageLimit(2);这样能提高ViewPager的一些流畅性,但是很多时候我们并不需要

加载下一个页面,我们希望切换到那一页才开始加载,也能为用户节省流量,所以就有了懒加载,懒加载说 
简单也简单,因为就一个方法,setUserVisibleHint如下

//会在oncreatView之前调用
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()){
            isVisble = true;
            onVisible();
        }else {
            isVisble = false;
            onInvisble();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

最近写些东西练手,用到了ViewPager+Fragment的组合,然后封装了一个基类,基本实现了懒加载功能,

当然可能会有一些不足,大家如果有好的想法欢迎指正,以下是整个Fragment的基类,代码不多,就简单封装了下,大家主要看懒加载吧

/**
 * A simple {@link Fragment} subclass.
 * fragment的封装基类
 */
public abstract class BaseFragment extends Fragment {
    protected Context mContext;
    protected View view;
    protected Unbinder unbinder;
    protected CompositeDisposable compositeDisposable = new CompositeDisposable();
    //是否可见
    protected boolean isVisble=false;
    //是否初始化完成
    protected boolean isPrepared = false;
    //是否第一次加载完成
    protected boolean isFirstLoaded = false;
    //第一个默认的fragment是否加载
    protected boolean isGankFirstLoad = false;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(setContent(), container,false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        //初始化结束后,设为true
        isPrepared = true;

        setListener();
        return view;
    }


    protected void setListener() {

    }
    //会在oncreatView之前调用
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()){
            isVisble = true;
            //可见的操作
            onVisible();
        }else {
            isVisble = false;
            onInvisble();
        }
    }

    protected void onInvisble() {

    }

    protected void onVisible() {
        //尚未初始化,返回去做初始化
        if (!isPrepared){
            return;
        }
        //只有在第一加载时才加载数据
        if (!isFirstLoaded){

            initData();
            isFirstLoaded = true;
        }
    }

    protected  abstract void initData();

    protected  void initView(){}
    //设置布局文件
    protected abstract int setContent();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        //页面销毁时重置
        isPrepared = false;
        isFirstLoaded = false;
        isGankFirstLoad = false;
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        if (compositeDisposable!=null&&compositeDisposable.size()>0){
            compositeDisposable.clear();
        }


    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101

一些说明,大家可能看到,我有一

//第一个默认的fragment是否加载 
protected boolean isGankFirstLoad = false;

这个我是后来测试后加上去的,就是在默认第一个fragment加载的时候,无法显示数据

在默认的fragment中是这么写的

 @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //此处第一次要额外调用一次数据加载,否则会没有数据
        if (!isGankFirstLoad){

            initData();
            isGankFirstLoad = true;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

猜你喜欢

转载自blog.csdn.net/tiankongcheng6/article/details/79088381
今日推荐