How to implement pagination in RecyclerView which is inside nestedScrollView

4.4k views Asked by At

I have included RecyclerView inside NestedScrollView and set

mRecyclerView.setNestedScrollingEnabled(false);

Since the scrolling of RecyclerView is set to false, I will not get the scroll position of RecyclerView due to this I will not be able to put pagination in RecyclerView.

I have also tried this

   mRecylerview.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) {

            if(dy > 0) {
                totalItemCount = linearLayoutManager.getItemCount();
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                    // End has been reached
                    // Do something
                    if (mOnLoadMoreListener != null) {
                        mOnLoadMoreListener.onLoadMore();
                        isLoading = true;
                    }

                }
            }
        }
    });

But here lastVisibleItem always gives the length of the list to be displayed.

After few digging, I found that when we use RecylerView inside NestedScrollView all the view gets created at the very beginning so that's the reason why lastVisibleItem always gives the size of the list.

Or if I am wrong please explain, me how RecyclerView inside NestedScrollView works?.

Is there any workaround so that I can get RecyclerView scroll position so that I can make my pagination work?.

It will be a great help. Thanks in advance

1

There are 1 answers

1
taman neupane On

Here scroll is NestedScrollView

scroll.getViewTreeObserver().addOnScrollChangedListener(() -> {
                View view = (View) scroll.getChildAt(scroll.getChildCount() - 1);

                Log.d("CategoryNeswList", scroll.getChildCount() + " child");
                int diff = (view.getBottom() - (scroll.getHeight() + scroll
                        .getScrollY()));

                if (diff == 0) {
//                        getPlaylistFromServer("more");
                    Toast.makeText(mContext, "Load More", Toast.LENGTH_SHORT).show();
                }
            });