Reputation: 1827
Help me please.
I have an Activity with Toolbar and fragment container (a <fragment>
, because I use Navigation Component
in app).
Goal: Scroll down Activity’s toolbar upon fragment’s content scroll. E. i. when fragment is scrolled far down, only the content of fragment is visible.
The standard solution of setting app:layout_scrollFlags="scroll|enterAlways|snap"
for toolbar
and app:layout_behavior="@string/appbar_scrolling_view_behavior"
for fragment
does not work.
Currently when I scroll down fragment, Activity's toolbar stays at place.
XML of Activity:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways|snap"/>
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="@+id/nav_host_carrier_root"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="false"
app:defaultNavHost="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:navGraph="@navigation/carrier_root_nav_graph" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Upvotes: 1
Views: 562
Reputation: 51
You need to use NestedScrollView and set:
app:layout_behavior="@string/appbar_scrolling_view_behavior"
this NSV could be the fragment parent or be inside a fragment inside your nav graph
Upvotes: 1