3

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

Abhishek Jaiswal
  • 588
  • 5
  • 17

1 Answers1

6

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();
                }
            });
taman neupane
  • 895
  • 9
  • 18
  • When it reaches the end of the screen it continuously calls getPlaylistFromServer("more"); method. There should be a way to stop this. – Sandeep Jun 23 '20 at 12:51