我实现了这一点,以获得无限滚动工作与我的回收器视图。我这样设置:
mRecyclerView.addOnScrollListener(new EndlessRecyclerViewScrollListener(glm) {
@Override
public void onLoadMore(int page, int totalItemsCount) {
anotherOne(page, mScope, mCurrentSortPreference, false);
Log.v(LOG_TAG, "onloadmore called");
}
});
方法anotherOne()
通过异步任务从API加载回收器视图中的数据。如果我有20+项从API(在初始调用中)获取,它工作得很好。这是因为我的网格允许在屏幕上同时显示最多20个结果。
但是,如果api返回的项目很少(如2或3),而不是只显示那些,则会重复调用onLoadMore()
,直到它用相同的2或3个项目填充屏幕。然后停止。
Schreenshot
这是我在EndlessRecyclerViewScrollListener.java中修改的唯一一行:
private int visibleThreshold = 1;
谢谢。
E: add code
private void anotherOne(int currPage, int scope, String sort, boolean o){
String override = Boolean.valueOf(o).toString();
(new FetchMoviesTask()).execute(Integer.valueOf(currPage).toString(),
Integer.valueOf(scope).toString(), sort, override, "");
}
来自源文档:
//当前滚动位置下方的最小项目数
//在加载更多。
private int visibleThreshold = 5;
这意味着它必须至少有visibleThreshold
更多的项目不被显示,为了安全起见,不收集更多的数据…所以你在请求:
private int visibleThreshold = 1;
这意味着你的数据集必须有"全屏+可见阈值",因为你没有更多的数据,它假设你有,请求错误的偏移量,并得到最后一个重复,直到你有"全屏+可见阈值"数据单位。
降低Treshold
,或在onLoadMore
上返回false
编辑:
再次从文档中,请尝试以下内容:@Override
public boolean onLoadMore(int page, int totalItemsCount) {
// Triggered only when new data needs to be appended to the list
// Add whatever code is needed to append new items to your AdapterView
loadNextDataFromApi(totalItemsCount);
return true; // ONLY if more data is actually being loaded; false otherwise.
}
public void loadNextDataFromApi(int offset) {
// Send an API request to retrieve appropriate paginated data
// --> Send the request including an offset value (i.e `page`) as a query parameter.
// --> Deserialize and construct new model objects from the API response
// --> Append the new data objects to the existing set of items inside the array of items
// --> Notify the adapter of the new items made with `notifyDataSetChanged()`
}
在我的情况下,我忘记添加行。希望能帮到别人。recyclerView.addOnScrollListener (scrollListener);
如果你在EndlessRecyclerViewScrollListener的onscroll()方法中设置了一个断点,你就会知道为什么。如果屏幕上没有足够的项目,则recyclerView将自动滚动,并调用onscroll方法。由于没有足够的项目占用屏幕,lastVisibleItemPosition +阈值将始终大于totalNumberOfItems。因此,onLoadMore方法将被反复调用。