SmartRefreshLayout pull down refresh and pull up to load more data processing

It is the most common to load and refresh paging data in the project. Using the SmartRefreshLayout control, the control is changed to a more comprehensive refresh control, which is worth learning

github address: https://github.com/scwang90/SmartRefreshLayout

1. The home page introduces the refresh control code base in lib-base (a project dedicated to the library):

api 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0'  //1.0.5及以前版本的老用户升级需谨慎,API改动过大

2. Next, write the layout in the layout file xml:

<com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/srl_deal_flow"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srlEnableScrollContentWhenLoaded="true"
        app:srlEnableFooterFollowWhenLoadFinished="true">

 // RecyclerView 控件

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

3. Not much to say about initializing controls,

4. Pull down to refresh and pull up to load more methods to call network data:

private int page = 1; 
// 下面10为offset 偏移量
mSrl_dealFlow.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mSrl_dealFlow.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        // 四个参数
                        getRechargeData(DateUtil.getYear(),DateUtil.getMonth(),page,10);
                        mSrl_dealFlow.finishRefresh();
                        mSrl_dealFlow.resetNoMoreData();//setNoMoreData(false);
                    }
                },2000);

            }
        });
        mSrl_dealFlow.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                mSrl_dealFlow.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (commonDealFlowAdapter.getCount() > 30) {
                            ToastUtil.show(getActivity(),"数据全部加载完毕");
                            refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                        } else {
                            page++;
                            // 两个参数
                            getRechargeData(page,10);
                            refreshLayout.finishLoadMore();
                        }
                    }
                },2000);
            }
        });

5. When initializing the List collection, it is necessary to initialize when initializing data:

 @Override
    public void initData(Bundle savedInstanceState) {
        mRechargeList = ObjectUtil.newArrayList();
        getRechargeData(1,10);
    }

6. When processing the data returned from the server, you need to pay attention to when the page value is 1, that is, the first page, you need to clear the data in the List collection, this time is to refresh the data,

private void processRechargeData(String str) {
        if(page == 1){
            mRechargeList.clear();
        }

 

 

 

 

 

 

 

 

 

Published 57 original articles · liked 0 · 10,000+ views

Guess you like

Origin blog.csdn.net/java9832/article/details/104829965