Android RecyclerView+SmartRefreshLayout 实现刷新和加载更多

https://github.com/scwang90/SmartRefreshLayout

RecyclerView+SmartRefreshLayout 实现刷新和加载更多

//1.1.0 (1.0.5及以前版本的老用户升级需谨慎,API改动过大)
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-31'
compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-31'//没有使用特殊Header,可以不加这行
compile 'com.android.support:appcompat-v7:25.3.1'//版本 23以上(必须)

xml

    

        

        

        
    

activity

    private void getBookBg() {
        getData();
        smartRefreshLayout
                .setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        page++;
                        getData();
                        refreshLayout.finishLoadMore(500);
                    }
                })
                .setOnRefreshListener(new OnRefreshListener() {
                    @Override
                    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                        page = 1;
                        getData();
                        refreshLayout.finishRefresh(500);
                    }
                });
    }
    
	BookBgOnlineAdapter adapter;

    private void getData() {
        HttpRequest2.getBookBg(page).subscribe(new ApiSubcriber>() {

            @Override
            public void onResult(ListResult result) {
                LogUtils.e(result.list);
                List list = result.list;
                if (list == null || list.size() == 0) {
                    return;
                }
                if (adapter == null || page == 1) {
                    adapter = new BookBgOnlineAdapter();
                    adapter.addData(result.list);
                    //
                    recyclerView.setAdapter(adapter);
                    recyclerView.setLayoutManager(new GridLayoutManager(activity, 2, GridLayoutManager.VERTICAL, false));
                } else {
                    adapter.addData(result.list);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailed(Result result) {
                Log.e(TAG, "onFailed: \n" + result.message);
            }

            @Override
            public void onCompleted() {
                super.onCompleted();
            }
        });
    }

你可能感兴趣的:(RecyclerView,Android基础,框架类库)