Rxjava一行代码为RecyclerView添加EmptyView

来个效果先

GIF.gif

简介

先说说需求场景吧,微信闪动,是产品的头像,mmp,一直程序猿本能的反映到,一定不是啥好事.果然,要给所有RecyclerView添加空数据展位图.我擦~为啥子不早说,早说用ListView了,好歹还内置了一个EmptyView功能.
看了看项目里的十多个RecyclerView,这可万万不能挨个修改布局啊.于是想到可以通过吧EmptyView作为一个item添加到RecyclerView中,刚好之前对列表类的都用了RxJava做了统一处理,于是很简单的就加上了,分享一下心得.
刷新布局 : SmartRefreshLayout (一个智能,便于自定义刷新头尾的框架)
https://github.com/scwang90/SmartRefreshLayout
通用Adapter https://github.com/hongyangAndroid/baseAdapter

实现

之前代码中做了关于上拉加载,关于是否还有下一页设置上拉加载布局的配置.

 RetrofitHelper.getApiWithUid().getEvaluationList(method, reviewed, page)
                .compose(RxScheduler.defaultScheduler())
                .doOnNext(new FinishLoadConsumer(refreshLayout, page))
                .subscribe(new BaseObserver(mActivity) {
                    @Override
                    public void error(Throwable e) {

                    }

                    @Override
                    public void next(EvaluationListResponse evaluationListResponse) {
                        if (page == 1)
                            data.clear();
                        data.addAll(evaluationListResponse.getData().getList());
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void complete() {

                    }
                });

用法类似这样,在FinishLoadConsumer 中通过后台的code判断如果为空数据,则添加null,在adapter中判断如果item的数据有,且为null,则添加空视图布局,把空数据视图的文案让后台同学传给你,就可以封装到基类里啦~



/**
 * Created by fushuang on 2017/12/22.
 */

public class   FinishLoadConsumer implements Consumer {

    private final RefreshLayout refreshLayout;
    private Integer currentPage;
    private List list;

    public FinishLoadConsumer(RefreshLayout refreshLayout) {
        this.refreshLayout = refreshLayout;
    }

    public FinishLoadConsumer(RefreshLayout refreshLayout, Integer currentPage) {
        this.refreshLayout = refreshLayout;
        this.currentPage = currentPage;
    }


    @Override
    public void accept(T t) throws Exception {

        RecyclerView recyclerView = (RecyclerView) ((SmartRefreshLayout) refreshLayout).getChildAt(0);
        if (t.getCode()!=0){

            RecyclerView.Adapter rv_adapter = recyclerView.getAdapter();
            if (rv_adapter instanceof CommonAdapter) {
                CommonAdapter adapter = (CommonAdapter) rv_adapter;
                adapter.getDatas().clear();
                adapter.getDatas().add(null);
                adapter.notifyDataSetChanged();
            }else if (rv_adapter instanceof HeaderAndFooterWrapper){
                HeaderAndFooterWrapper headerAndFooterWrapper = (HeaderAndFooterWrapper) rv_adapter;
                headerAndFooterWrapper.mInnerAdapter.getDatas().clear();
                headerAndFooterWrapper.mInnerAdapter.getDatas().add(null);
                headerAndFooterWrapper.notifyDataSetChanged();
            }

        }else {

            refreshLayout.setEnableLoadmore(true);
            refreshLayout.setEnableLoadmore(true);

            if (currentPage!=null&¤tPage>=t.getData().getSum_page()){
                refreshLayout.setEnableLoadmore(false);
            }else {
                refreshLayout.setEnableLoadmore(true);
            }
            refreshLayout.finishLoadmore();
            refreshLayout.finishRefresh();
        }

    }
}
 
 

关于adapter就不做展示了,只是默认添加一个null.
弘扬大神在adapter 中通过mItemViewDelegateManager 管理多种布局,每次多一种类型的布局添加一个ItemViewDelegate,通过isForViewType,convert分别控制是否应该加载当前布局,应该怎么加载.这种方式还是很好用的
RxJava 在网络请求中的链式数据处理,对于处理这种问题也是极为方便O(∩_∩)O,在请求的末端,只需要插装自己需要功能的Consumer,即可便捷完成各种功能,灵活的封装.

你可能感兴趣的:(Rxjava一行代码为RecyclerView添加EmptyView)