Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination not work for the RecyclerView within NestedScrollView

Tags:

How to implement pagination of recyclerview that is within NestedScrollView?

like image 781
Jatin Avatar asked Oct 09 '17 04:10

Jatin


People also ask

What is difference between nested ScrollView and NestedScrollView?

Nested scrolling is enabled by default. Show activity on this post. NestedScrollView is just like ScrollView, but in NestedScrollView we can put other scrolling views as child of it, e.g. RecyclerView. But if we put RecyclerView inside NestedScrollView, RecyclerView's smooth scrolling is disturbed.

What is RecyclerView pagination?

Pagination with Recyclerview allows the user to see the latest content as page by page. As we load the next 'page' by the time users scroll to the bottom, more content is loaded and available.


2 Answers

Follow this steps :

1. Set nested scrolling enabled false of recycler view.

recyclerView.setNestedScrollingEnabled(false); 

2. Add scroll listner to nested scrollview.

 mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {            @Override            public void onScrollChanged()            {                     View view = (View)mScrollView.getChildAt(mScrollView.getChildCount() - 1);                      int diff = (view.getBottom() - (mScrollView.getHeight() + mScrollView                                     .getScrollY()));                      if (diff == 0) {                        // your pagination code                     }            }   }); 
like image 184
Vidhi Dave Avatar answered Sep 22 '22 16:09

Vidhi Dave


if you are using Kotlin your code will be looks like

 scroll?.viewTreeObserver?.addOnScrollChangedListener {         val view = scroll.getChildAt(scroll.childCount - 1)         Timber.d("Count==============${scroll.childCount}")          val diff = view.bottom - (scroll.height + scroll.scrollY)         Timber.d("diff==============$diff")          if (diff == 0) {             //your api call to fetch data         }     } 

and last but the not the least set RecyclerView scrolling false

 ViewCompat.setNestedScrollingEnabled(recyclerView, false) 
like image 44
shahid17june Avatar answered Sep 22 '22 16:09

shahid17june