导航抽屉项目单击

时间:2020-02-13 15:17:18

标签: android android-fragments navigation-drawer drawerlayout

我有 DrawerLayout NavigationView 。当我单击导航抽屉中的项目时,有两个片段会更改,并且效果很好:

    <fragment
            android:id="@+id/nav_settings"
            android:label="@string/menu_settings"
            tools:layout="@layout/fragment_settings" />

    <fragment
            android:id="@+id/nav_themes"
            android:label="@string/menu_themes"
            tools:layout="@layout/fragment_themes" />

问题是我在抽屉菜单中还有其他几项不是“片段”,所以我无法使它们可点击:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:showIn="navigation_view">

    <group
            android:id="@+id/group_1"
            android:checkableBehavior="single">
        <item
                android:id="@+id/nav_settings"
                android:icon="@drawable/ic_keyboard_settings"
                android:title="@string/menu_settings" />
        <item
                android:id="@+id/nav_themes"
                android:icon="@drawable/ic_theme"
                android:title="@string/menu_themes" />
    </group>
    <group android:id="@+id/group_2">
        <item
                android:id="@+id/nav_developer_page"
                android:icon="@drawable/ic_developer_page"
                android:title="@string/menu_developer_page" />
        <item
                android:id="@+id/nav_privacy_policy"
                android:icon="@drawable/ic_privacy_policy"
                android:title="@string/menu_privacy_policy" />
    </group>
</menu>

代码如下:

        setSupportActionBar(toolbar)
        toggle = object : ActionBarDrawerToggle(this, drawer_layout, toolbar, 0, 0) {
            override fun onDrawerClosed(drawerView: View) {
                super.onDrawerClosed(drawerView)
                syncState()
            }

            override fun onDrawerOpened(drawerView: View) {
                super.onDrawerOpened(drawerView)
                syncState()
            }
        }
        toggle.syncState()
        drawer_layout.addDrawerListener(toggle)

        val navController = findNavController(R.id.nav_host_fragment)
        appBarConfiguration = AppBarConfiguration(setOf(
                R.id.nav_settings, R.id.nav_themes, R.id.nav_developer_page), drawer_layout)
        nav_view.setupWithNavController(navController)
        toggle.syncState()

设置 NavigationItemSelectedListener 时,它会中断片段导航。

如何使这两个项目可点击并调用函数?

1 个答案:

答案 0 :(得分:1)

解决办法是从导航视图中获取菜单项并设置一个点击监听器:

    val signoutMenuItem = binding.nvNavigationDrawerNavigationView.menu.findItem(id.navigation_drawer_menu_sign_out)
    signoutMenuItem.setOnMenuItemClickListener {
        navigationDrawerMainActivityViewModel.signOut()
        true
    }

并且不要在 AppBarConfiguration 中包含非片段项:

    appBarConfiguration = AppBarConfiguration(
        setOf(
            id.drawerFragmentX,
            id.drawerFragmentY,
            //id.navigation_drawer_menu_sign_out <- Do NOT include
        ), drawerLayout
    )
    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)

此解决方案与 https://stackoverflow.com/a/59451345/5189200 相同。

哈维