首先导入
1 | implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.50' |
上拉下拉,选择布局SmartRefreshLayout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <com.scwang.smartrefresh.layout.SmartRefreshLayout android:id="@+id/ly_pull_refresh" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/color_white" android:orientation="vertical" app:srlReboundDuration="600"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recharge_list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/color_f7f7f7" android:overScrollMode="never" android:scrollbars="none"/> </com.scwang.smartrefresh.layout.SmartRefreshLayout> |
初始化控件,并创建一个BaseQuickAdapter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | static final int PAGE_SIZE = 10;//每次加载10项 int page = 1;//加载第一页 RechargeListItemAdapter mRechargeListItemAdapter; @BindView(R.id.ly_pull_refresh) SmartRefreshLayout mRefreshLy; @BindView(R.id.recharge_list) RecyclerView mRechargeListRV; public class RechargeListItemAdapter extends BaseQuickAdapter<RechargeListBean, BaseViewHolderExt> { public RechargeListItemAdapter(@Nullable List<RechargeListBean> data) { super(R.layout.fragment_recharge_list_item, data); } @Override protected void convert(@NonNull BaseViewHolderExt helper, RechargeListBean item) { helper.setText(R.id.status_str, item.getStatus_str()) .setText(R.id.money, item.getMoney()) .setText(R.id.op_name, item.getOp_name()) .setText(R.id.created_time, item.getCreated_time()); } } |
设置下拉刷新:
1 2 3 4 | mRefreshLy.setOnRefreshListener(refreshLayout -> { page = 1;//设为默认加载第一页 selectBill(false, false);//获取数据 }); |
设置adapter:
1 2 3 4 5 6 7 8 9 10 11 12 13 | public void onLazyInitView(@Nullable Bundle savedInstanceState) { super.onLazyInitView(savedInstanceState); mRechargeListItemAdapter = new RechargeListItemAdapter(null); mRechargeListItemAdapter.setEmptyView(new YJGEmptyView(getContext(), YJGEmptyView.TYPE_ORDER));//为空时布局 mRechargeListItemAdapter.setLoadMoreView(new YJGLoadMoreView());//上来加载时,加载的布局 mRechargeListItemAdapter.setEnableLoadMore(false);//默认设置不能加载更多 mRechargeListItemAdapter.setPreLoadNumber(3); mRechargeListItemAdapter.setOnLoadMoreListener(() -> {//设置加载更多监听 selectBill(false, true);//获取数据 }); mRechargeListRV.setAdapter(mRechargeListItemAdapter); selectBill(true, false);//初始化获取参数 } |
获取参数的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | public void selectBill(boolean loading, boolean isLoadMore){ if (loading) {//刚进入页面时,需要显示加载菊花 showLoading(); } RetrofitClient.getInstance().getApiService() .getRechargeList(page,PAGE_SIZE)//获取网络数据参数 .map(new ResultTransformFunction<>()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(RechargeListBean -> { hideLoading();//隐藏菊花 mRefreshLy.finishRefresh();//完成刷新 if (isLoadMore) { mRechargeListItemAdapter.loadMoreComplete();//网络请求成功的话调用loadMoreComplete() } if (RechargeListBean != null && RechargeListBean.size() > 0) {//有了请求数据 if (isLoadMore) {//为true时,是加载更多获取的数据 mRechargeListItemAdapter.addData(RechargeListBean);//加载更多的数据,add进去 } else { mRechargeListItemAdapter.setNewData(RechargeListBean);//进入或者上拉加载的数据(page=1),setnewdata } Log.v("re22","33333==="+PAGE_SIZE+"=="+RechargeListBean.size()); if (RechargeListBean.size() == PAGE_SIZE) {//获取的数据未10条时,说明还有下一页,page加1,以备下次请求 page++; mRechargeListItemAdapter.setEnableLoadMore(true);//允许加载更多 } else { mRechargeListItemAdapter.loadMoreEnd(true);//若这次获得的数据没有10条,说明全部加载完毕。 } } Log.v("aaa2","=="+RechargeListBean.size()); }, throwable -> { hideLoading(); mRefreshLy.finishRefresh(); if (isLoadMore) { mRechargeListItemAdapter.loadMoreFail();//请求失败的话调用loadMoreFail() } showToast(throwable.getMessage()); }); } |
就这样,就实现了上拉和下拉。
但是有个疑问,adapter是怎么监听上拉加载的呢,也就是setOnLoadMoreListener是怎么执行的呢。
1 2 3 | mRechargeListItemAdapter.setOnLoadMoreListener(() -> {//设置加载更多监听 selectBill(false, true);//获取数据 }); |
换一种写法:
1 2 3 4 5 | mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() { @Override public void onLoadMoreRequested() { } }); |
也就是,这个onLoadMoreRequested是如何执行的。
通过参考资料BaseQuickAdapter和BaseMultiItemQuickAdapter使用,
里面对源码的分析,有一个关键方法:
1 | autoLoadMore(position); |
在getItemViewType里面,有个autoLoadMore(position);,在2.9.50版本,这个方法在onBindViewHolder里面。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //通过position,来判断还有没有更多内容,然后调用onLoadMoreRequested private void autoLoadMore(int position) { if (this.getLoadMoreViewCount() != 0) { if (position >= this.getItemCount() - this.mPreLoadNumber) { if (this.mLoadMoreView.getLoadMoreStatus() == 1) { this.mLoadMoreView.setLoadMoreStatus(2); if (!this.mLoading) { this.mLoading = true; if (this.getRecyclerView() != null) { this.getRecyclerView().post(new Runnable() { public void run() { BaseQuickAdapter.this.mRequestLoadMoreListener.onLoadMoreRequested(); } }); } else { this.mRequestLoadMoreListener.onLoadMoreRequested(); } } } } } } |
参考资料BaseQuickAdapter和BaseMultiItemQuickAdapter使用