onNavigationItemSelected not calling when item is selected

后端 未结 2 1031
南笙
南笙 2021-01-29 14:02

I am adding footer-view in navigation drawer using following code -




        
2条回答
  •  旧巷少年郎
    2021-01-29 14:31

    You attach setNavigationItemSelectedListener with NavigationView which will override during default configuration of setupWithNavController. So attach your listener after configuring default settings. Check below code.

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

    Update: To work with default navigation you have to handle like below:

    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
    }
    

提交回复
热议问题