【问题标题】:onNavigationItemSelected not calling when item is selected选择项目时 onNavigationItemSelected 不调用
【发布时间】:2020-02-05 09:46:29
【问题描述】:

我正在使用以下代码在导航抽屉中添加页脚视图 -

<?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
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nv_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|start">

        <androidx.core.widget.NestedScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <include layout="@layout/nav_header_main"/>

                <com.google.android.material.navigation.NavigationView
                    android:id="@+id/drawer_menu_body"
                    app:elevation="0dp"
                    android:layout_height="0dp"
                    android:layout_width="wrap_content"
                    android:layout_weight="1"
                    app:menu="@menu/activity_main_drawer">

                </com.google.android.material.navigation.NavigationView>

                <include layout="@layout/navigation_drawer_bottom_view"/>
            </LinearLayout>

        </androidx.core.widget.NestedScrollView>
    </com.google.android.material.navigation.NavigationView>


</androidx.drawerlayout.widget.DrawerLayout>

但是现在 onNavigationItemSelected() 没有调用导航抽屉也保持打开状态,直到我们手动滑动它。我正在使用导航组件和导航图。

这是我的活动代码 -

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val toolbar: Toolbar = findViewById(R.id.toolbar)
    setSupportActionBar(toolbar)

    val navController = findNavController(R.id.nav_host_fragment)
    drawer_menu_body.setNavigationItemSelectedListener(this)
    nv_top.setNavigationItemSelectedListener(this)

    appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.nav_home,
            R.id.nav_gallery,
            R.id.nav_slideshow,
            R.id.nav_tools,
            R.id.nav_share,
            R.id.nav_send
        ), drawer_layout)


    setupActionBarWithNavController(navController, appBarConfiguration)
    drawer_menu_body.setupWithNavController(navController)
    nv_top.setupWithNavController(navController)
}

override fun onNavigationItemSelected(menu: MenuItem): Boolean {
    Log.d("testing_navigation","testing_navigation")
    drawer_layout.closeDrawer(GravityCompat.START)
    return true
}

【问题讨论】:

    标签: android menu navigation navigation-drawer


    【解决方案1】:

    您将setNavigationItemSelectedListener 附加到NavigationView,这将在setupWithNavController 的默认配置期间覆盖。因此,在配置默认设置后附加您的侦听器。检查下面的代码。

    nv_top.setupWithNavController(navController)
    drawer_menu_body.setNavigationItemSelectedListener(this)
    

    更新:要使用默认导航,您必须按如下方式处理:

    override fun onNavigationItemSelected(menu: MenuItem): Boolean {
        val handled = NavigationUI.onNavDestinationSelected(menu, navController)
    
        if (!handled) {
            // handle other navigation other than default
        }
    
        drawer_layout.closeDrawer(GravityCompat.START)
    
        return handled
    }
    

    【讨论】:

    • 是的,您还可以与默认导航合并。但那是不同的事情。更改是否调用您的onNavigationItemSelected
    • 是的,它现在正在工作,但是我如何将默认导航与其合并,请帮忙。
    • 谢谢@Md。 Asaduzzaman,我在这个问题上浪费了 1 天时间。文档中没有提到 nav-controller 和 itemselection listener 的顺序。
    【解决方案2】:

    亲爱的,这对我有用的项目 R.id.nav_logout 我需要完成活动而不是实例片段:

    private lateinit var appBarConfiguration: AppBarConfiguration
    var drawerLayout: DrawerLayout? = null
    var navView: NavigationView? = null
    var navController: NavController? = null
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_customer_main)
        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)
    
        val fab: FloatingActionButton = findViewById(R.id.fab)
        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show()
        }
    
        drawerLayout = findViewById(R.id.drawer_layout)
        navView = findViewById(R.id.nav_view)
        navController = findNavController(R.id.nav_host_fragment)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        appBarConfiguration = AppBarConfiguration(setOf(
                R.id.nav_home, R.id.nav_service_requests), drawerLayout)
        setupActionBarWithNavController(navController!!, appBarConfiguration)
        navView!!.setupWithNavController(navController!!)
        navView!!.setNavigationItemSelectedListener(this)
    }
    
    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }
    
    override fun onNavigationItemSelected(menuItem: MenuItem): Boolean {
        menuItem.isChecked = true
        drawerLayout!!.closeDrawers()
        when (menuItem.itemId) {
            R.id.nav_logout -> {
                Prefs.putBoolean("flagLogin", false)
                val intent = Intent(this@CustomerMainActivity, LoginActivity::class.java)
                startActivity(intent)
                finish()
                return true
            }
        }
        if( navController!!.currentDestination!!.id != menuItem.itemId)
            navController!!.navigate(menuItem.itemId)
        return true
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多