Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView ScrollListener inside NestedScrollView

I have an EndlessRecyclerView at the end of a NestedScrollView. EndlessRecyclerView means: when user scrolls to the bottom of the recyclerView it loads more data. This is already implemented and working elsewhere but when I put the recyclerView inside the NestedScrollView the OnScrollListener events doesn't fire.

XML design:

<NestedScrollView>       <Other views/>       <EndlessRecyclerView/>  </NestedScrollView > 

Code:

recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {         @Override         public void onScrolled(RecyclerView recyclerView, int dx, int dy) {             super.onScrolled(recyclerView, dx, dy);             // This is never fired! Here is where I implement the logic of EndlessRecyclerView         }     }); 

How do I get scroll event for above case?

I know that is not good to have two scrollable views inside each other. But, how do I have the above case without having two scrollable views?

I already followed this link but it doesn't work: scroll event for recyclerview inside scrollview android

like image 585
Damia Fuentes Avatar asked Oct 06 '16 11:10

Damia Fuentes


People also ask

How do I prevent NestedScrollView from scrolling if body is small?

The problem can be solved by moving the SliverAppBar into the CustomScrollView and not use the NestedScrollView at all.

What is nested scroll view?

NestedScrollView is just like ScrollView , but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

What is nestedScrollingEnabled?

android:nestedScrollingEnabled="true" in the nested (child) scrollable view, assuming you have one somewhere inside another. This causes the child view to scroll to completion, and then allow its parent to consume the rest of the scroll.


1 Answers

To achieve endless scrolling for recycler view which is under NestedScrollView, you can use "NestedScrollView.OnScrollChangeListener"

nestedScrollView.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {             if(v.getChildAt(v.getChildCount() - 1) != null) {                 if ((scrollY >= (v.getChildAt(v.getChildCount() - 1).getMeasuredHeight() - v.getMeasuredHeight())) &&                         scrollY > oldScrollY) {                         //code to fetch more data for endless scrolling                 }             }         }); 

Here v.getChildCount() -1 should give you the recycler view for which you be implementing endless scrolling.

Also scrollY > oldScrollY confirms that the page is being scrolled down.

Reference: NestedScrollView.OnScrollChangeListener

like image 141
Govind Avatar answered Oct 01 '22 11:10

Govind