我正在使用RecyclerView来显示项目列表。总共有10个项目,但每次只能看到3个。但是当我使用recyclerView.getChildCount()方法获取可见计数时,它会给我10而不是3。可能出了什么问题。我正在尝试实现分页。所以每次我的可见计数都与totalitemcount相同,结果我的load more一直被调用,直到所有页面都加载完毕。以下是我的代码。
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
onScrolled();
}
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLinearLayoutManager.getItemCount();
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading
&& (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
current_page++;
onLoadMoreData(current_page);
loading = true;
}
}