【问题标题】:How to expand AppBarLayout/Toolbar everytime fragment destination changes每次片段目标更改时如何扩展 AppBarLayout/Toolbar
【发布时间】:2019-11-26 18:00:42
【问题描述】:

我正在构建包含大量片段的单一活动应用程序,并且我正在使用 Jetpack 导航组件。我在工具栏中有app:layout_scrollFlags="scroll|enterAlways",因此当用户滚动内容时,工具栏将可以滚动以提供足够的空间,并且效果很好。
但是在一个片段中,我在 Action Bar 菜单 中有 SearchView,因此当用户从具有可滚动内容的片段导航到此搜索片段时,SearchView 和其他菜单项将是自动隐藏。 如何在创建/显示每个片段时使 ActionBar/Toolbar 可见。

我的活动布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.activities.MainActivity">

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

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

        <include layout="@layout/content_main" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="@android:color/white"
        app:menu="@menu/main_bottom" />
</RelativeLayout>

我的片段布局示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.fragments.notification.NotificationsFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

【问题讨论】:

    标签: android android-layout android-fragments


    【解决方案1】:

    这很简单。我通过在 NavController 上添加监听器解决了这个问题

            navController.addOnDestinationChangedListener { _, _, _ ->
                appBarLayout.setExpanded(true, true)
            }
    

    【讨论】:

      猜你喜欢
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 2016-03-12
      • 2015-11-10
      • 1970-01-01
      相关资源
      最近更新 更多