NavHostFragment包含非滚动视图时,将CoordinatorLayout作为顶级装饰 [英] CoordinatorLayout as top-level decor when NavHostFragment contains non-scrolling views

查看:88
本文介绍了NavHostFragment包含非滚动视图时,将CoordinatorLayout作为顶级装饰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个在活动中具有共享chrome(工具栏等)的单活动应用.

我想使用Google的组件( CoordinatorLayout AppBarLayout )来利用工具栏的自动隐藏功能,该功能在滚动内容时隐藏/显示.这对于应用程序的主页"目标至关重要,该应用程序具有一个在每个页面上具有可滚动内容的视图分页器.

我已将 NavHostFragment layout_behavior 设置为建议的滚动行为,但是当非滚动片段占据 NavHostFragment .在这种情况下,将片段的内容下推(切掉底部),以便为活动布局中的工具栏腾出空间.

活动布局:

 < androidx.coordinatorlayout.widget.CoordinatorLayoutandroid:id ="@ + id/coordinator_layout"android:layout_width ="match_parent"android:layout_height ="match_parent">< com.google.android.material.appbar.AppBarLayoutandroid:id ="@ + id/app_bar_layout"android:layout_width ="match_parent"android:layout_height ="wrap_content"app:elevation ="0dp">< com.google.android.material.appbar.MaterialToolbarandroid: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><片段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> 

具有滚动内容(在每个页面上带有 RecyclerView ViewPager2 )的目标位置可以按预期工作.工具栏会按预期隐藏/显示.

但是,当我导航到另一个没有滚动内容的目的地时(只是一个高度设置为 match_parent ConstraintLayout ,子视图固定在底部和布局的顶部),内容在底部被切除.

不能在非滚动目标上添加底部填充,因为共享工具栏可以在输入该目标时扩展(因为它可能在前一个目标中已折叠),并且仅在动画显示时将其向下推./p>

我看到的问题是 NavHostFragment 位于工具栏的下方,而不是在其后面(由于滚动视图的行为).

我不确定该怎么办.缠住我的头,同时还要担心窗框的插入,这是脖子上的疼痛.我正在考虑只是放弃 CoordinatorLayout 作为共享镶边,而仅将其添加到具有滚动内容的目标中.这样会限制我可以在目标之间使用哪些动画,但这似乎是目前最好的选择.

有什么想法吗?

解决方案

在onCreate上对您的Activity进行尝试 setSupportActionBar(工具栏)或者 setActionBar(toolbar)

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?

解决方案

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

这篇关于NavHostFragment包含非滚动视图时,将CoordinatorLayout作为顶级装饰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆