簡體   English   中英

導航抽屜項目點擊

[英]Navigation Drawer item click

我有DrawerLayoutNavigationView 當我單擊導航抽屜中的項目時,有兩個片段會發生變化,並且效果很好:

    <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" />

問題是我在抽屜菜單中還有其他幾個不是 Fragments 的項目,我無法使它們可點擊:

<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 時,它會中斷 Fragments 的導航。

如何使這兩個項目可點擊並調用函數?

解決方案是從導航視圖中獲取菜單項並設置一個點擊監聽器:

    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相同。

哈維

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM