首页 > 解决方案 > 带有导航组件的 DrawerLayout:非主页顶级目的地上的汉堡菜单不会加载抽屉

问题描述

我正在我的单活动导航组件设置中设置 DrawerLayout,与此处指南中的概述非常相似。

这是我的主要活动的代码:

class MainActivity : AppCompatActivity(), SubjectFragment.OnListFragmentInteractionListener {
    lateinit var drawerLayout: DrawerLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        drawerLayout = findViewById<DrawerLayout>(R.id.main_drawer_layout)
        val navView = findViewById<NavigationView>(R.id.main_navigation_view)
        val sroToolbar = findViewById<Toolbar>(R.id.main_toolbar)
        val navController = findNavController(R.id.nav_host_fragment)

        navView.setupWithNavController(navController)
        setSupportActionBar(sroToolbar)

        val appBarConfiguration = AppBarConfiguration(
            setOf(R.id.subjectFragment, R.id.aboutFragment, R.id.settingsFragment),
            drawerLayout
        )
        setupActionBarWithNavController(navController, appBarConfiguration)

    }

    override fun onSupportNavigateUp(): Boolean {
        return findNavController(R.id.nav_host_fragment).navigateUp(drawerLayout)
    }

上面,我使用 AppBarConfiguration 设置三个顶级目的地(subjectFragment、aboutFragment、settingsFragment),其中 subjectFragment 是导航图中的主目的地。这是我的导航图:

<?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/sro_nav_graph"
    app:startDestination="@id/subjectFragment">

    <fragment
        android:id="@+id/subjectFragment"
        android:name="black.old.spacedrepetitionowl.SubjectFragment"
        android:label="Spaced Repetition Owl: Subjects"
        tools:layout="@layout/fragment_subject_list" >
        <action
            android:id="@+id/action_subjectFragment_to_aboutFragment"
            app:destination="@id/aboutFragment" />
        <action
            android:id="@+id/action_subjectFragment_to_settingsFragment"
            app:destination="@id/settingsFragment" />
    </fragment>
    <fragment
        android:id="@+id/aboutFragment"
        android:name="black.old.spacedrepetitionowl.AboutFragment"
        android:label="Spaced Repetition Owl: About"
        tools:layout="@layout/fragment_about" />
    <fragment
        android:id="@+id/settingsFragment"
        android:name="black.old.spacedrepetitionowl.settingsFragment"
        android:label="fragment_settings"
        tools:layout="@layout/fragment_settings" />
</navigation>

当我启动应用程序时,它会显示 subjectFragment,点击时汉堡菜单会打开抽屉。到目前为止,这是正确的行为。然后我的抽屉会显示三个项目:主题、设置和关于。

当我从菜单中选择设置/关于时,这会正确打开设置/关于片段。

我的问题是,当我在这两个屏幕上时,点击汉堡图标将加载 SubjectFragment,而不是让抽屉显示出来。这是发生的事情的GIF:

汉堡包图标不显示抽屉内容

我想要的行为是每次在每个目的地点击汉堡菜单时都会加载抽屉。这里可能是什么问题?

标签: androidkotlinnavigation-drawerdrawerlayoutandroid-architecture-navigation

解决方案


啊哈!我通过将其与使用“导航抽屉活动”作为项目模板制作的新项目进行比较来解决这个问题。

事实证明,appBarConfiguration需要将其用作参数,navigateUp以便遵守顶级目的地设置:

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }

推荐阅读