I am using RecyclerView to show list of items. The total items are 10, but visible at a time is 3. But when i use recyclerView.getChildCount() method to get the visible count, it is giving me 10, instead of 3. What can be the issue. I am trying to implement pagination. So everytime my visible count is coming same as totalitemcount, and as a result my load more is getting called continuously till all the pages are loaded. Below is my code.
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;
}
}
Check this answer: Get visible items in RecyclerView
LinearLayoutManager layoutManager = ((LinearLayoutManager)mRecyclerView.getLayoutManager());
int visibleItemCount = layoutManager.findLastVisibleItemPosition() - layoutManager.findFirstVisibleItemPosition();
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With