使用 CoordinatorLayout、Toolbar 和 fragment 在 fragment 之间切换时如何使工具栏出现

How to make the toolbar appear when switching between fragments using CoordinatorLayout, Toolbar and fragment

我正在使用下面的布局,CoordinatorLayout 包含在其中 AppBarLayout(带工具栏)和 fragment 布局。

我在我的工具栏中使用 app:layout_scrollFlags,在片段中使用 app:layout_behavior="@string/appbar_scrolling_view_behavior",因此工具栏会随着片段内容的滚动而出现和消失 (hide/show)。

我的问题: 当我在片段 A 中滚动并因此工具栏消失,然后我使用 BottomNavigationView 转到片段 B 时工具栏仍然隐藏。 我希望每次打开新片段时都显示工具栏

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/containerMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/AppBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_gravity="top"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
        </com.google.android.material.appbar.AppBarLayout>
            <fragment
                android:id="@+id/nav_host_fragment"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:defaultNavHost="true"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:navGraph="@navigation/mobile_navigation" />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="@dimen/nav_bar"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:itemIconTint="@drawable/bottom_color_nav"
        app:itemTextColor="@drawable/bottom_color_nav"
        app:layout_constraintBottom_toBottomOf="@id/main_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

我假设您发布的 xml 是 MainActivity() 的布局,如果是这样,请在您的 MainActivty 中创建对工具栏的引用,并在您的 MainActivty() 中创建一个函数 access/change 您的工具栏折叠状态。

在您的 Fragment 中,创建对 MainActivity 的引用,然后访问该函数以更改工具栏的状态。

我无法在工作中进行测试,但是当仅使用具有内部片段的 MainActivity 作为应用程序的导航流时,我使用了相同的代码流来执行相同的操作。

下面不是确切的工作或测试代码,但相同 idea/flow

//my fragment function to access MainActivity().changeToolbarState(boolean)
changeToolbar(state: boolean) {
    if(state){
      //true
      //show toolbar
      MainActivty().changeToolbarState(true)

    }else{
      //false
      //collapse/hidetoolbar
     MainActivty().changeToolbarState(false)
    }
}

最好的替代方法是使用片段中的以下代码将应用栏布局的 setExpanded 属性 设置为 true。这里,MainActivity.appBarLayout是Main中定义的静态变量Activity,引用了App Bar Layout。

@Override
public void onPause() {
    super.onPause();
    MainActivity.appBarLayout.setExpanded(true);
}