美文网首页
下拉刷新整合空白页方案

下拉刷新整合空白页方案

作者: 西门只会吹雪 | 来源:发表于2018-12-21 15:31 被阅读0次

    1.说明

    2.使用方法

    布局文件:

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
                android:id="@+id/refreshLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:visibility="visible"
                app:srlEnablePreviewInEditMode="true">
    
                <com.chailijun.example.EmptyLayout
                    android:id="@+id/empty_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/white">
    
                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/detail_recyclerView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:nestedScrollingEnabled="false"
                        android:scrollbars="none"
                        android:visibility="visible" />
    
                </com.chailijun.example.EmptyLayout>
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
    
    
    
    

    java 代码:

            mRefreshLayout.setEnableRefresh(true);
            mRefreshLayout.setEnableLoadMore(true);
            //设置上次更新时间的key
            String lastUpdateTimeKey = "lastUpdateTimeKey";//每个页面该字符串不能相同
            mClassicsHeader = new ClassicsHeader(mContext, lastUpdateTimeKey);
            mRefreshLayout.setRefreshHeader(mClassicsHeader);
            mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(RefreshLayout refreshLayout) {
                    refreshNewData();
                }
            });
            mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore(RefreshLayout refreshLayout) {
                    loadMoreData();
                }
            });
    

    在获取数据后:

    //显示没有网络布局
    EmptyLayoutManager.getInstance().showEmptyView(mEmptyLayout, EmptyType.NO_NET);
    
    //显示没有数据布局,其他类似布局可以依次扩展
    EmptyLayoutManager.getInstance().showEmptyView(mEmptyLayout, EmptyType.NO_DATA);
    
    //显示内容布局
    EmptyLayoutManager.getInstance().showContent(mEmptyLayout);
    

    3.其他代码

    EmptyType

    @IntDef({
            EmptyType.NO_NET,
            EmptyType.NO_DATA,
            EmptyType.NO_NEWS
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface EmptyType {
    
        /**
         * 没有网络
         */
        int NO_NET = 0;
    
        /**
         * 暂无相关数据
         */
        int NO_DATA = 1;
    
        /**
         * 暂无相关资讯
         */
        int NO_NEWS = 2;
    
    }
    

    EmptyLayoutManager

    public class EmptyLayoutManager {
    
        private static EmptyLayoutManager instance;
    
        private EmptyLayoutManager(){
    
        }
    
        public static EmptyLayoutManager getInstance(){
            if (instance == null){
                synchronized (EmptyLayoutManager.class){
                    if (instance == null){
                        instance = new EmptyLayoutManager();
                    }
                }
            }
            return instance;
        }
    
        private String emptyText = null;
    
        public void setEmptyText(String emptyTips) {
            this.emptyText = emptyTips;
        }
    
        public void showEmptyView(EmptyLayout emptyLayout, @EmptyType int type) {
            if (emptyLayout == null) {
                throw new NullPointerException("emptyLayout is null");
            }
            switch (type) {
                case EmptyType.NO_NET://没有网络
                    emptyLayout.setEmptyText("网络已断开").setEmptyImage(R.drawable.blank_no_net).showEmpty();
                    break;
    
                case EmptyType.NO_DATA:
                    String tips = "暂无相关数据";
                    if (!TextUtils.isEmpty(emptyText)){
                        tips = emptyText;
                    }
                    emptyLayout.setEmptyText(tips).setEmptyImage(R.drawable.blank_wuxiangguan).showEmpty();
                    break;
    
                case EmptyType.NO_NEWS:
                    emptyLayout.setEmptyText("暂无相关资讯").setEmptyImage(R.drawable.blank_wuxiangguan).showEmpty();
                    break;
    
                default:
                    break;
            }
        }
    
       public void showContent(EmptyLayout emptyLayout){
            if (emptyLayout == null) {
                throw new NullPointerException("emptyLayout is null");
            }
            emptyLayout.showContent();
        }
    }
    

    ClassicsHeader
    自定义下拉刷新头部局代码略,可参考这里

    public class ClassicsHeader extends LinearLayout implements RefreshHeader {
    }
    

    相关文章

      网友评论

          本文标题:下拉刷新整合空白页方案

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