android - android中嵌套图形片段中的控制工具栏后退按钮和汉堡图标
问题描述
我所有的抽屉片段都通过 MainFragment 控制,在 MainFragment 内部实现了一个 DrawerLayout,其中包含 DashboardMainFragment、SettingFragment 和 PrivacyFragment。我的要求是将带有菜单的工具栏显示到抽屉的每个子项中。所以 MainFragment 看起来像:
主要片段.kt
@AndroidEntryPoint
class MainFragment :
BaseFragment<FragmentMainBinding>(FragmentMainBinding::inflate) {
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var drawerLayout: DrawerLayout
private lateinit var navigationView: NavigationView
private lateinit var navController: NavController
override fun initViews() {
drawerLayout = binding.drawerLayout
navigationView = binding.navView
val nestedNavHostFragment =
childFragmentManager.findFragmentById(R.id.nav_host_fragment_content_main) as? NavHostFragment
navController = nestedNavHostFragment?.navController!!
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_dashboard, R.id.navigation_settings, R.id.navigation_privacy
), drawerLayout
)
NavigationUI.setupWithNavController(
binding.appBarNav.toolbar,
navController,
appBarConfiguration
)
navigationView.setupWithNavController(navController)
inflateMenuInToolbar()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
private fun inflateMenuInToolbar() {
binding.appBarNav.toolbar.inflateMenu(R.menu.dashboard_menu)
}
fun clearToolbarMenu() {
binding.appBarNav.toolbar.menu.clear()
}
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
android:id="@+id/app_bar_nav"
layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_nav"
app:menu="@menu/drawer_nav_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.Shopingo.AppBarOverlay">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/Theme.Shopingo.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_nav">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment_content_main"
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:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/drawer_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
抽屉导航.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mobile_navigation2"
app:startDestination="@+id/navigation_dashboard">
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.next.shopingo.ui.fragments.main.home.drawer.DashboardMainFragment"
android:label="Dashboard" />
<fragment
android:id="@+id/navigation_settings"
android:name="com.next.shopingo.ui.fragments.main.home.drawer.SettingsFragment"
android:label="Settings" />
<fragment
android:id="@+id/navigation_privacy"
android:name="com.next.shopingo.ui.fragments.main.home.drawer.PrivacyFragment"
android:label="Privacy Policy" />
</navigation>
fragment_dashboard_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment_dashboard_main"
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:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/dashboard_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
仪表板导航.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/dashboard_navigation"
app:startDestination="@id/dashboardFragment">
<fragment
android:id="@+id/dashboardFragment"
android:name="com.next.shopingo.ui.fragments.main.home.dashboard.DashboardFragment"
android:label="Dashboard Content"
tools:layout="@layout/fragment_dashboard">
<action
android:id="@+id/dashboard_frag_to_detail_frag"
app:destination="@id/detailFragment" />
</fragment>
<fragment
android:id="@+id/detailFragment"
android:name="com.next.shopingo.ui.fragments.main.home.dashboard.DetailFragment"
android:label="Detail"
tools:layout="@layout/fragment_detail" />
</navigation>
现在堆栈管理工作正常,就像在抽屉父片段(DashboardMain,设置,隐私)之间切换一样。现在,当我在包含 DashboardFragment、DashboardDetailFragment 等片段的 DashboardMainFragment 中添加嵌套图并从 DashboardFragment 导航到 DashboardDetailFragment 时,工具栏不会自动显示后退按钮。它仍然显示汉堡图标。
我的问题是,Jetpack Navigation 中是否有任何方法可以使工具栏自动获得显示后退按钮的所有权,并且还可以处理嵌套图形和父图形的导航控制器。