Pagination using retrofit to fetch next 10 items from API on scrolling in android

Viewed 12851

Pagination using retrofit to fetch next 10 items from API on scrolling in android

4 Answers

Using RecyclerView.OnScrollListener and RecyclerView.LayoutManager you can find index of the last visible item. If the index equals to the total items count - 1 you can consider it as the end of the list and load the next page.

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        if (dy > 0) //check for scroll down
        {
            int totalItemCount = layoutManager.getItemCount();
            int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
            if (!loading && lastVisibleItemPosition == totalItemCount - 1) {
                loading = true;
                currentPage = currentPage + 1;
                if (!dynamicPages.equalsIgnoreCase("")) {
                    loadMoreData(currentPage);
                }
            }
        }
    }
});
Related