简体   繁体   English

如果我使用导航组件单击导航抽屉中的菜单,如何执行操作而不是移动到另一个目的地?

[英]how to perform an action instead of moving to another destination if I click a menu in Navigation Drawer using Navigation Component?

I am trying to use Jetpack Navigation component.我正在尝试使用 Jetpack Navigation 组件。 so navigation component will automatically handle if I want to segue from a destination to another destination when I click a menu in the navigation drawer因此,当我单击导航抽屉中的菜单时,如果我想从一个目的地转到另一个目的地,导航组件将自动处理

but I want to perform an action if a menu is clicked, say for example if a menu in drawer is clicked then I want to show a toast message.但是我想在单击菜单时执行一个操作,例如,如果单击抽屉中的菜单,那么我想显示一条吐司消息。

in old way, ie using fragment transaction, I can easily check from onNavigationItemSelected , but I am no longer find that method以旧方式,即使用片段事务,我可以轻松地从onNavigationItemSelected检查,但我不再找到该方法

在此处输入图片说明

so how to do that in navigation component ?那么如何在导航组件中做到这一点呢?

I have tried to check onDestinationChanged , but it doesn't work我试图检查onDestinationChanged ,但它不起作用

override fun onDestinationChanged(controller: NavController, destination: NavDestination, arguments: Bundle?) {

        if (destination.id == R.id.my_destination {

           // show toast in here
           // but it doesn't work
          
        }

    }

here is my MainActivity这是我的主要活动

class MainActivity : AppCompatActivity() {

    private lateinit var navController : NavController
    private lateinit var appBarConfiguration: AppBarConfiguration


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        appBarConfiguration = AppBarConfiguration(setOf(
            R.id.destination_share,
            R.id.destination_message,
            R.id.destination_chat),
            drawer_layout
        )


        // init nav controller
        navController = Navigation.findNavController(this,R.id.nav_host_fragment)

        // set toolbar
        setSupportActionBar(toolbar)

        // set up navigation drawer
        NavigationUI.setupActionBarWithNavController(this,navController, appBarConfiguration)
        NavigationUI.setupWithNavController(navigation_view,navController)

    }

    


    override fun onSupportNavigateUp(): Boolean {
        return NavigationUI.navigateUp(navController,appBarConfiguration)
    }

        
    }

You can handle Menu clicks as following您可以按如下方式处理菜单点击

     navView.menu.findItem(R.id.logout)
                .setOnMenuItemClickListener { menuItem: MenuItem? ->
                 // write your code here
                    true
                }

Have you tried using你有没有试过使用

bottom_nav_view.setOnNavigationItemSelectedListener {

    if (destination.id == R.id.my_destination {

       // show toast in here
       // but it doesn't work    
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击导航抽屉默认菜单上的项目时如何打开另一个菜单 - how to open another menu on click of item on the default menu of navigation drawer 使用导航组件时,如何在运行时为通过导航视图菜单访问的目的地设置参数? - When using Navigation Component, how do I set an argument at runtime for a destination accessed via navigation view menu? 如何打开关闭导航抽屉,单击图像视图。我不使用操作栏? - how to open close navigation drawer click on image view.i am not using action bar ?. 如何在导航架构中导航抽屉标题操作的任何目的地 - How to Navigate any destination for action of header of drawer in navigation architecture 使用ListView代替菜单的导航抽屉 - Navigation drawer with ListView instead menu 使用带有导航组件的导航抽屉时如何更改操作栏图标(汉堡图标) - how to change Action bar icon (hamburger icon) when using navigation drawer with navigation component Navigation Drawer 和 Bottom Navigation 菜单类别依赖 - Navigation Architecture 组件 - Navigation Drawer and Bottom Navigation menu category dependency - Navigation Architecture Component 使用导航抽屉作为滑动菜单 - Using Navigation Drawer as sliding menu 如何在导航抽屉中将整个操作栏菜单更改为适当的片段? - How to change the whole Action Bar menu to appropriate fragment in navigation drawer? 单击菜单项后,我的导航抽屉没有关闭? - My navigation Drawer not close after I click the menu item?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM