简体   繁体   中英

How to scroll away Activity's toolbar by scrolling its Navigation Component fragment

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM