2

i am making android app in which i am performing pagination with recyclerView. Basically i have two recylerView in my fragment. I am performing pagination with recylerview addOnScrollListener with one recyclerView. but it is not working fine. when i make this condition

if(dy > 0 ){

}

then this condition become false nothing implements and when i remove this condition then all pages loaded without scrolling. how i can do this. i want this when i scroll down a recyclerview then next page will be load. here is my code:

private void recyclerPagination() {
    rvRecProduct.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (dy > 0) {
                linearLayoutManager = (GridLayoutManager) rvRecProduct.getLayoutManager();
                visibleItemCount = recyclerView.getChildCount();
                totalItemCount = linearLayoutManager
                        .getItemCount();
                firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
                if (loading) {
                    if (totalItemCount > previousTotal) {
                        previousTotal = totalItemCount;
                        page++;
                        loading = false;
                    }
                }
                if (page <= limit) {
                    if (!loading && (firstVisibleItem + visibleThreshold + visibleItemCount) >= totalItemCount) {
                        loading = true;
                        getPagination();
                        Log.e("PageNO",String.valueOf(page));
                    }
                }
            }
        }
    });
}
Zoe
  • 23,712
  • 16
  • 99
  • 132
Anser Abbas
  • 59
  • 2
  • 11
  • 1
    do NOT reinvent the wheel - use google's [paging](https://developer.android.com/topic/libraries/architecture/paging.html) support library instead – pskink Oct 25 '18 at 10:42
  • Did you have two RecycerVew inside NestedScrollView? – ILLIA DEREVIANKO Oct 25 '18 at 10:43
  • Yes,both are in nested scrollView – Anser Abbas Oct 25 '18 at 10:44
  • 1
    @AnserAbbas check this [Pagination not work for the RecyclerView within NestedScrollView](https://stackoverflow.com/questions/46638779/pagination-not-work-for-the-recyclerview-within-nestedscrollview/46638845#46638845) – AskNilesh Oct 25 '18 at 10:45
  • @AnserAbbas check this link. It helped me early https://stackoverflow.com/questions/48428793/recyclerview-inside-nestedscrollview-does-not-give-correct-visible-item-position – ILLIA DEREVIANKO Oct 25 '18 at 11:01

1 Answers1

1

Android Pagination Using Recycler View To Load More than 10 Pages Pagination Scroll Listener Class:

    public abstract class PaginationScrollListener extends RecyclerView.OnScrollListener {

    LinearLayoutManager layoutManager;

    public PaginationScrollListener(LinearLayoutManager layoutManager) {
    this.layoutManager = layoutManager;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    int visibleItemCount = layoutManager.getChildCount();
    int totalItemCount = layoutManager.getItemCount();
    int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();

    if (!isLoading() && !isLastPage()) 
    {
        if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
                && firstVisibleItemPosition >= 0) 
        {
            loadMoreItems();
        }
    }

    }

    protected abstract void loadMoreItems();

    public abstract boolean isLastPage();

    public abstract boolean isLoading();}

Add this code in your Activity or Fragment:

public static final int PAGE_START = 1;
private int CURRENT_PAGE = PAGE_START;
private boolean isLoading = false, isLastPage = false;

private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;

recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);

recyclerView.setAdapter(new MyAdapter(list))
recyclerView.addOnScrollListener(new PaginationScrollListener(linearLayoutManager) {
                    @Override
                    protected void loadMoreItems() {
                        isLoading = true;
                        CURRENT_PAGE++;
                        loadNextPage();
                    }

                    @Override
                    public boolean isLastPage() {
                        return isLastPage;
                    }

                    @Override
                    public boolean isLoading() {
                        return isLoading;
                    }
                });