美文网首页
android懒加载fragment实现

android懒加载fragment实现

作者: CarlWu_S | 来源:发表于2017-07-11 10:29 被阅读0次

    偶遇一个不错的懒加载fragment实现,仅记录。

    public abstract class LazyFragment extends BaseFragment {
    
        protected boolean isViewInitiated; //控件是否初始化完成
        protected boolean isVisibleToUser; //页面是否可见
        protected boolean isDataInitiated; //数据是否加载
    
    
        @Override
        public void setUserVisibleHint(boolean isVisibleToUser) {
            super.setUserVisibleHint(isVisibleToUser);
            this.isVisibleToUser = isVisibleToUser;
            prepareFetchData(false);
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            isViewInitiated = true;
            prepareFetchData(false);
        }
    
        public abstract void loadData();
    
    
        protected void prepareFetchData(boolean forceUpdate) {
            if (isVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
                loadData();
                isDataInitiated = true;
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:android懒加载fragment实现

          本文链接:https://www.haomeiwen.com/subject/ngvphxtx.html