Android 如何在每次片段目标更改时展开AppBarLayout/工具栏
我正在构建一个包含很多片段的单一活动应用程序,我正在使用Jetpack导航组件。我在工具栏中有Android 如何在每次片段目标更改时展开AppBarLayout/工具栏,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,我正在构建一个包含很多片段的单一活动应用程序,我正在使用Jetpack导航组件。我在工具栏中有app:layout\u scrollFlags=“scroll | enterally”,这样当用户滚动内容时,工具栏将能够滚动到足够的空间,并且工作正常。 但是在一个片段中,我在操作栏菜单中有搜索视图,因此当用户从具有可滚动内容的片段导航到此搜索片段时,搜索视图和其他菜单项将自动隐藏。 如何在创建/显示每个片段时使ActionBar/工具栏可见 我的活动布局 <?xml version="1.
app:layout\u scrollFlags=“scroll | enterally”
,这样当用户滚动内容时,工具栏将能够滚动到足够的空间,并且工作正常。但是在一个片段中,我在操作栏菜单中有搜索视图,因此当用户从具有可滚动内容的片段导航到此搜索片段时,搜索视图和其他菜单项将自动隐藏。 如何在创建/显示每个片段时使ActionBar/工具栏可见 我的活动布局
<?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>
这很简单。我通过在NavController上添加侦听器解决了这个问题
navController.addOnDestinationChangedListener { _, _, _ ->
appBarLayout.setExpanded(true, true)
}