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();
}
});
You can try this approach:
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) //check for scroll down
{
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisibleItems = mLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
loading = false;
if (adapter.countOfShowing < adapter.allChallenges.size()) {
Log.e("...", "Last Item Wow !");
adapter.increaseCountOfShowing();
adapter.notifyDataSetChanged();
}
loading = true;
//Do pagination.. i.e. fetch new data
}
}
}
}
});
https://github.com/kunal-mahajan/PaginationAdeptor
I have implemented the pagination component / widget and that is very simple to implement. No xml is required only need to extend the class. Example is available in above url with timer task and dummy data. In main activity you may change the number of data available for testing purpose in "final int totalRecords"; If there is any issue will answer your queries. Thanks Kunal