The reason is that NestedScrollView does not call the AppBarLayout.Behavour class when it receives scroll events from recyclerview. that is, when scrolling occurs in recyclerview, recyclerview sends / skips the scroll progress to NestedScrollView . NestedScrollView receives a scroll event, but does nothing with it.
Inside the nestedscrollview class
@Override public void onNestedpreScroll(View target,int dx, int dy, int[] consumed){
To overcome this and make appbarlayout an extension / crash when scrolling through recyclerview, simply create your own class that extends NestedScrollView and overrides the dispatchNestedPreScroll() method and call method described above that informs appbarlayout of the scroll event and allows it to respond to it.
public class CustomNestedScrollView extends NestedScrollView{ public CustomNestedScrollView(Context context) { super(context); } public CustomNestedScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public void onNestedPreScroll(View target, int dx, int dy, int[] consumed){ dispatchNestedPreScroll(dx,dy,consumed,null); } }
And then use this class in your layout.xml
<com.my.package.CustomNestedScrollView android:id="@+id/scroll" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> /* CONTENT */ </LinearLayout>
source share