带导航组件的DrawerLayout:非家庭顶层目标上的汉堡包菜单不会加载抽屉

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

我正在单活动导航组件设置中设置DrawerLayout,大致与on the guide here概述的一样。

这是我主要活动的代码:

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:

hamburger icon not showing the drawer content

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

android kotlin navigation-drawer drawerlayout android-architecture-navigation
1个回答
0
投票

啊哈!我将其与使用“导航抽屉活动”作为项目模板的新项目进行了比较,从而得出了结论。

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

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }
© www.soinside.com 2019 - 2024. All rights reserved.