如下源码方法:
public void loadMoreComplete() {
if (getLoadMoreViewCount() == 0) {
return;
}
mLoading = false;
mNextLoadEnable = true;
mLoadMoreView.setLoadMoreStatus(LoadMoreView.STATUS_DEFAULT);
notifyItemChanged(getLoadMoreViewPosition());
}
当调用notifyItemChanged(getLoadMoreViewPosition());时又会调用onBindViewHolder()方法 此时关键代码来了,此时又重新调用了 autoLoadMore(position);方法,该方法就会出问题
private void autoLoadMore(int position) {
if (getLoadMoreViewCount() == 0) {
return;
}
if (position < getItemCount() - mPreLoadNumber) {
return;
}
if (mLoadMoreView.getLoadMoreStatus() != LoadMoreView.STATUS_DEFAULT) {
return;
}
mLoadMoreView.setLoadMoreStatus(LoadMoreView.STATUS_LOADING);
if (!mLoading) {
mLoading = true;
if (getRecyclerView() != null) {
getRecyclerView().post(new Runnable() {
@Override
public void run() {
mRequestLoadMoreListener.onLoadMoreRequested();
}
});
} else {
mRequestLoadMoreListener.onLoadMoreRequested();
}
}
}
由于此时调用了loadMoreComplete()方法,mLoading处于false状态,由此会进入下边的判断,造成无限回掉加载更多。。。。。。。这就尴尬了!不光这个方法,loadMoreEnd()....记不清楚了那几个结束加载的方法也是如此, 我遇到的问题场景:对于RecyclerView的item我没有设置满一屏幕,而我设置了加载更多监听,在里边我就设置了上拉加载添加数据,结果竟然有那么多数据,忘记说了我也设置了不满一屏幕时不加载更多虽然有顺序问题,因为在你设置监听时就会开启加载更多,说的有点乱,看一下代码吧!希望有帮助!
private void init(View view) {
mRvContent = view.findViewById(R.id.rv_main_content);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
mRvContent.setLayoutManager(layoutManager);
ArrayList<CardBean.CardListEntity> data = new ArrayList<>();
mAdapter = new SingleItemAdapter(R.layout.my_item, data);
mRvContent.setAdapter(mAdapter);
initData();
if (mAdapter.isLoadMoreEnable()) {
mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
@Override
public void onLoadMoreRequested() {
mAdapter.addData(mCardBean.cardList);
mAdapter.loadMoreEnd();
}
}, mRvContent);
}
}
private void initData() {
String json = CommenUtils.getJson(NewsApp.getmContext(), "cardlist.json");
mCardBean = CommenUtils.jsonToBean(json, CardBean.class);
mAdapter.replaceData(mCardBean.cardList);
mAdapter.setPreLoadNumber(8);
mAdapter.disableLoadMoreIfNotFullPage(mRvContent);
}
上边的mAdapter.disableLoadMoreIfNotFullPage(mRvContent);在这设置没有一点卵用。希望有更好的写法的小伙伴指出不足!