baseRecyclerViewAdapterHelper框架使用
一、上拉加载更多
上拉加载更多一直有坑没有解决,就是第一次加载数据会回调加载跟多方法,其次是上拉过程中加载跟多会被回调多次
解决个方案
其实,仔细研读官方的方案会发现,在设置完成适配器之后应当默认调用,
mAdapter.loadMoreEnd(true);
mAdapter.loadMoreComplete();
,亦或者是serviceSeachAdapter.loadMoreFail();
以此来最终完成整个加载动作
完成的代码过程
1.设置开启加载更多
serviceSeachAdapter.bindToRecyclerView(rvSurrounnding);
serviceSeachAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
@Override
public void onLoadMoreRequested() {
currentPage += 1;
isLoadMore = true;
loadData();
}
},rvSurrounnding);
2.默认第一次加载会进入回调,如果不需要可以配置:
serviceSeachAdapter.disableLoadMoreIfNotFullPage();
2.在数据请求的回调中设置本次完成的状态
serviceList = responsebody.getResult().getServiceList();
if (isLoadMore) {
isLoadMore = false;
serviceSeachAdapter.addData(serviceList);
} else {
serviceSeachAdapter.setNewData(serviceList);
}
if (serviceList.size() < PAGE_COUNT) {
serviceSeachAdapter.loadMoreEnd(true);
}else{
serviceSeachAdapter.loadMoreComplete();
}
3.在error的回调中应当设置加载失败
serviceSeachAdapter.loadMoreFail()
参数说明
加载完成(注意不是加载结束,而是本次数据加载结束并且还有下页数据)
mQuickAdapter.loadMoreComplete();
加载失败
mQuickAdapter.loadMoreFail();
加载结束
mQuickAdapter.loadMoreEnd();
注意:如果上拉结束后,下拉刷新需要再次开启上拉监听,需要使用setNewData方法填充数据。
打开或关闭加载(一般用于下拉的时候做处理,因为上拉下拉不能同时操作)
mQuickAdapter.setEnableLoadMore(boolean);
预加载(这个功能屌炸天)
// 当列表滑动到倒数第N个Item的时候(默认是1)回调onLoadMoreRequested方法
mQuickAdapter.setPreLoadNumber(int);
设置自定义加载布局####
mQuickAdapter.setLoadMoreView(new CustomLoadMoreView());
代码如下
public final class CustomLoadMoreView extends LoadMoreView {
@Override
public int getLayoutId() {
return R.layout.view_load_more;
}
/**
* 如果返回true,数据全部加载完毕后会隐藏加载更多
* 如果返回false,数据全部加载完毕后会显示getLoadEndViewId()布局
*/
@Override
public boolean isLoadEndGone() {
return true;
}
@Override
protected int getLoadingViewId() {
return R.id.load_more_loading_view;
}
@Override
protected int getLoadFailViewId() {
return R.id.load_more_load_fail_view;
}
/**
* isLoadEndGone()为true,可以返回0
* isLoadEndGone()为false,不能返回0
*/
@Override
protected int getLoadEndViewId() {
return 0;
}
}
xml文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_40">
<LinearLayout
android:id="@+id/load_more_loading_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ProgressBar
android:id="@+id/loading_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleSmall"
android:layout_marginRight="@dimen/dp_4"
android:indeterminateDrawable="@drawable/sample_footer_loading_progress"/>
<TextView
android:id="@+id/loading_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_4"
android:text="@string/loading"
android:textColor="#0dddb8"
android:textSize="@dimen/sp_14"/>
</LinearLayout>
<FrameLayout
android:id="@+id/load_more_load_fail_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<TextView
android:id="@+id/tv_prompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#0dddb8"
android:text="@string/load_failed"/>
</FrameLayout>
</FrameLayout>
查看代码发现,BaseQuickAdapter 通过 bindToRecyclerView() 方法,绑定RecyclerView ———> 获取到layoutmanager———>监听条目填充变化——>判断是最后一个条目——>开启监听,(判断是不是loadmore状态和enableLoadMore,是通过
mAdapter.loadMoreEnd(true);
mAdapter.loadMoreComplete();
,serviceSeachAdapter.loadMoreFail();
三个方法结束loadmore状态的)产生回调