Nestedscrollview: !!top!!
Enter NestedScrollView . It is the sophisticated, collaborative sibling of the classic ScrollView , designed specifically to solve nested scrolling problems. NestedScrollView is a FrameLayout that extends ScrollView but implements NestedScrollingParent and NestedScrollingChild interfaces. In plain English: It can both receive scroll events from its children and coordinate scroll events with its parent.
By attaching the appbar_scrolling_view_behavior to the NestedScrollView , the scroll events automatically trigger the AppBarLayout to expand or collapse. If you are using Jetpack Compose, the equivalent is Modifier.nestedScroll . The same principles apply: nestedscrollview
<TextView ... /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" android:nestedScrollingEnabled="true" /> <!-- Note this --> Enter NestedScrollView
<androidx.coordinatorlayout.widget.CoordinatorLayout> <com.google.android.material.appbar.AppBarLayout> <com.google.android.material.appbar.CollapsingToolbarLayout app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView ... /> <androidx.appcompat.widget.Toolbar ... /> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout> In plain English: It can both receive scroll
</LinearLayout> </androidx.core.widget.NestedScrollView> For the magic to work, the inner scrollable view (e.g., RecyclerView ) must have nested scrolling enabled. In modern AndroidX versions, this is true by default, but you can explicitly set it: