CoordinatorLayout as top-level decor when NavHostFragment contains non-scrolling views

一笑奈何 提交于 2020-12-12 10:14:11

问题


I'm attempting to make a single-activity app with shared chrome (toolbar, etc) in the activity.

I'd like to use Google's components (CoordinatorLayout and AppBarLayout) to make use of the toolbar's auto-hide feature, which hides/reveals when content is scrolled. This is crucial for the "home" destination of the app which has a view pager with scrollable content on each page.

I've set the NavHostFragment's layout_behavior to the suggested scrolling behavior, but this doesn't work well when a non-scrolling fragment is occupying the NavHostFragment. In such a case, the content of the fragment is pushed down (cutting off the bottom portion) to make room for the toolbar in the activity layout.

Activity layout:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|snap|enterAlways"/>

    </com.google.android.material.appbar.AppBarLayout>

    <fragment
        android:id="@+id/nav_host_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:navGraph="@navigation/nav_graph_main"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

The home destination with scrolling content (ViewPager2 with a RecyclerView on each page) works as intended. The toolbar hides/reveals as expected.

However, when I navigate to another destination that doesn't have scrolling content (it's just a ConstraintLayout with height set to match_parent and child views anchored to the bottom and top of the layout), the content is cut off at the bottom.

Adding bottom padding to the non-scrolling destination isn't an option because the shared toolbar can expand upon entering this destination (as it may have been collapsed in the previous destination) and just pushes the content down as it animates.

The problem I'm seeing is that the NavHostFragment is positioned below the toolbar, rather than behind it (due to the scrolling view behavior).

I'm not sure what to do. Wrapping my head around this while also worrying about window insets is a pain in the neck. I'm considering just abandoning the CoordinatorLayout as shared chrome and adding it only to the destinations that have scrolling content. It would limit what animations I can use between destinations, but that seems to be the best option at this point.

Any thoughts?


回答1:


try it on your Activity in onCreate setSupportActionBar(toolbar) or setActionBar(toolbar)



来源:https://stackoverflow.com/questions/59847836/coordinatorlayout-as-top-level-decor-when-navhostfragment-contains-non-scrolling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!