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));
                    }
                }
            }
        }
    });
}
5

1 Answer 1

2

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;
                    }
                });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.