How to implement pagination of recyclerview
that is within 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.
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.
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 } } });
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With