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

标签 java android android-fragments android-coordinatorlayout android-appbarlayout

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

我在工具栏中使用 app:layout_scrollFlags 并在 fragment 中使用 app:layout_behavior="@string/appbar_scrolling_view_behavior",因此工具栏出现并消失(隐藏/show) 并滚动我的 fragment 内容。

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

<?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() 中创建一个函数来访问/更改工具栏折叠状态。

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

我无法像在工作中那样进行测试,但当仅使用带有内部 fragment 的 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)
    }
}

关于java - 如何使用CoordinatorLayout、Toolbar和fragment在fragment之间切换时出现工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61110479/

相关文章:

java - hbase api - 通过行 ID 列表获取数据行信息

java - ListView getFirstVisiblePosition

java - 带有 Spring boot jpa 错误 : "Unable to extract OUT/INOUT parameter value" 的 SQL 服务器过程调用

android - 如何重命名 Firebase 数据库中的子项?

Android:使用 viewPager 限制 fragment 加载

android - v4 支持库是否在可用时使用新类?

java - 不在 Hibernate 中使用 EntityManager 的后果

java - 异常 java.lang.NoClassDefFoundError android/app/Activity

Android ProgressDialog 进度条按正确的顺序做事

android - 多步骤流程中的单个 Activity 多个 fragment/单独的 Activity ?