安卓中实现分页加载方案总结(一)——基于SmartRefreshLayout

背景

项目中多场景使用到了列表分页加载的操作,比如消息列表,先总结一下基于SmartRefreshLayout的分页加载方案。

实现方案

一、导入SmartRefreshLayout相关jar包

// 下拉刷新
    implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0'
    implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0'

二、xml布局文件引入SmartRefreshLayout

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ...>
        <com.scwang.smartrefresh.layout.SmartRefreshLayout
                android:id="@+id/refreshLayout"
                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" />


                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rv_list"    
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/transparent"
                    android:overScrollMode="never"
                    tools:listitem="@layout/list_message_item" />

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

            </com.scwang.smartrefresh.layout.SmartRefreshLayout>
         ...
    </LinearLayout>

三、activity(fragment)中进行相应调用

public class MyActivity extends PermissionActivity implements View.OnClickListener{
    ...
    ...
    // 1、page变量,标记每次请求的页面number
    private int page = 1;
    private SmartRefreshLayout refreshLayout;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_my_carrier;
    }

    @Override
    protected void initWidget() {
        ...
        ...
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                prePage = page;
                // 刷新的时候也是加载第一页的数据,和第一次进入一致
                page = 1;
                refreshLayout.setEnableLoadMore(true);
                requestWeb();
            }
        });
        // 2、加载更多监听,通过page获取新增page的页面数据
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                requestWeb();
            }
        });
    }

    @Override
    protected void intiData() {
        refreshLayout.autoRefresh();
    }

    // 请求网络数据
    private void requestWeb(){
            // 通过传入page和pageSize参数从后台获取该页的数据。
            RetrofitHelper.getInstance().getInformation(page, 10)
                .subscribe(new BaseObserver<PageInfo<Information>>() {
                    @Override
                    public void onNext(@NonNull PageInfo<Information> rsp) {
                        loading.setValue(false);
                        if (null != rsp.getList()) {
                            // 3、获取查询到的数据,增加到我们的data数组中。
                            infoMutableLiveData.setValue(rsp);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        loading.setValue(false);
                    }
                });
    }
    ...
    ...
}

总结

本文简单总结了下,基于SmartRefreshLayout实现分页加载和刷新的思路,可供参考,持续优化。