[使用CoordinatorLayout,工具栏和片段在片段之间切换时如何显示工具栏

问题描述 投票:0回答:1

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

我在工具栏中使用app:layout_scrollFlags,在片段中使用app:layout_behavior="@string/appbar_scrolling_view_behavior",因此通过滚动我的片段内容,工具栏出现和消失(隐藏/显示)。

我的问题:当我在片段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>

java android android-fragments android-coordinatorlayout android-appbarlayout
1个回答
0
投票

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

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

我无法从工作中进行测试,但是当仅使用带有内部片段的MainActivity作为应用程序的导航流时,我使用相同的代码流来做相同的事情。

下面不是确切的工作或测试代码,而是相同的想法/流程

//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)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.