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

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/smartRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.scwang.smartrefresh.layout.header.ClassicsHeader
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <com.scwang.smartrefresh.layout.footer.ClassicsFooter
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srlClassicsSpinnerStyle="Translate" />
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

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<ListResult<BookBackground>>() {

            @Override
            public void onResult(ListResult<BookBackground> result) {
                LogUtils.e(result.list);
                List<BookBackground> 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();
            }
        });
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值