6

I created Recycler View Grid. And I want realize pagination. But I do not understand how do it. I Found one answer enter link description here

But it not work for me. I have not method mLayoutManager.findFirstVisibleItemPosition(); in my LayOutManager. And method mRecyclerView.setOnScrollListener is deprecated. How can I realize pagination in Recycler View Grid?

Community
  • 1
  • 1
ip696
  • 6,574
  • 12
  • 65
  • 128
  • 1
    For one thing, findFirstVisibleItemPosition is a method of the LinearLayoutManager implementation, and you just need to use addOnScrollListener. – Eric Cochran Jun 25 '15 at 08:25
  • Thank you for addOnScrollListener. What about GridLayoutManager? How can i do it? – ip696 Jun 25 '15 at 08:31
  • GridLayoutManager is a subclass of LinearLayoutManager. GLM has the findFirstVisibleItemPosition method. – Eric Cochran Jun 26 '15 at 07:55

3 Answers3

1

Late answer but you can try this..and It works perfectly

allMovie_recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() {
        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
            if (dy > 0) {

                val layoutManager = allMovie_recycler_view.layoutManager as GridLayoutManager
                val visibleItemCount = layoutManager.findLastCompletelyVisibleItemPosition()+1
                if (visibleItemCount == layoutManager.itemCount){
                    //Load more data
                }


            }
        }
    })
Musfick Jamil
  • 126
  • 1
  • 3
-1

Here is the proper way to paginate with StaggeredGridLayoutManager.

The only difference is in findFirstVisibleItemPositions(), which returns an int[] for the first visible position in each span.

mRecyclerView.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);
        int visibleItemCount = mGridLayoutManager.getChildCount();
        int totalItemCount = mGridLayoutManager.getItemCount();
        int[] firstVisibleItemPositions = mGridLayoutManager.findFirstVisibleItemPositions(null);

        if (!mIsLoading && !mIsLastPage) {
            if ((firstVisibleItemPositions[0] + visibleItemCount) >= totalItemCount
                && firstVisibleItemPositions[0] >= 0
                && totalItemCount >= Config.PAGE_SIZE) {
                loadMorePosts();
            }
        }
    }
});
Eduard
  • 3,482
  • 2
  • 27
  • 45
  • findFirstVisibleItemPosition return a integer not a int[] also it is 'findFirstVisibleItemPosition' not 'findFirstVisibleItemPositions' – Subho Mar 06 '18 at 07:47
-3

I know the answer is late. For others who may encounter this, use this solution.

mRecyclerView.addOnScrollListener(mRecyclerViewOnScrollListener);

private RecyclerView.OnScrollListener
        mRecyclerViewOnScrollListener = 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);
        int visibleItemCount = layoutManager.getChildCount();
        int totalItemCount = layoutManager.getItemCount();
        int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();

        if (!isLoading && currentPage < totalPages) {
            if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
                    && firstVisibleItemPosition >= 0
                    && totalItemCount >= numberOfItemsInAPage) {
                loadMoreItems();
            }
        }
    }
};
Sreeraj P K
  • 100
  • 4