I created a RecyclerView for my application and trying to find a way to stop huge requests, I want to get the results 10 by 10 with a progress bar. My server will send the r
What we did was use ReyclerView.OnScrollListener. Assuming you're using a LinearLayoutManager, you can check the current shown item versus the total count to determine when you've reached the last item, then request the next page. You also need to throw in some additional logic checks to early out to prevent "spamming" as the scroll even happens a lot.
For example:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (!hasMoreContent()) {
return;
}
if (currentlyLoadingInitialRequest()) {
return;
}
if (alreadyLoadingNextPage()) {
return;
}
if (isInErrorState()) {
return;
}
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
int total = layoutManager.getItemCount();
int currentLastItem = layoutManger.findLastVisibleItemPosition();
if (currentLastItem == total - 1) {
requestNextPage();
}
});