[英]AndroidX JetPack NavigationUI - Overriding Action on Non-Navigation Item In Drawer
I just started using Android NavigationUI to implement Fragment Navigation through a drawer menu.我刚开始使用 Android NavigationUI 通过抽屉菜单实现 Fragment Navigation。 While it does feel a bit like magic, I have one main issue with it.
虽然它确实感觉有点像魔术,但我有一个主要问题。 That is, I have a
logout
item in the menu that should not Navigate to any Fragment, but rather to an "action".也就是说,我在菜单中有一个
logout
项,它不应导航到任何片段,而是导航到“操作”。 While I found similar questions here such as this one, I haven't found a clear answer to my question yet.虽然我在这里找到了类似的问题,比如这个,但我还没有找到我的问题的明确答案。 The following is the function I'm currently using to setup the navigation:
以下是我目前用于设置导航的功能:
private fun setupNav() {
val binding: ActivityMainMenuBinding = DataBindingUtil.setContentView(this,
R.layout.activity_main_menu)
drawerLayout = binding.drawerLayout
navController = Navigation.findNavController(this, R.id.content_frame)
appBarConfiguration = AppBarConfiguration(navController.graph, drawerLayout)
// Set up ActionBar
setSupportActionBar(binding.toolbar)
setupActionBarWithNavController(navController, appBarConfiguration)
binding.navView.setupWithNavController(navController)
}
If you have any suggestions on how I could approach this, it would be truly appreciated if you could share them.如果您对我如何解决这个问题有任何建议,如果您能分享它们,将不胜感激。
You just need to add below mentioned code您只需要添加下面提到的代码
nav_view.menu.findItem(R.id.nav_logout).setOnMenuItemClickListener {
Log.e("=>"," You clicked on logout !!! ")
true
}
and Here is my full code这是我的完整代码
class NavigationDrawerActivity : AppCompatActivity() {
private lateinit var appBarConfig: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_navigation_drawer)
setSupportActionBar(toolbar)
val navController: NavController = findNavController(R.id.nav_host_fragment)
appBarConfig = 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, appBarConfig)
nav_view.setupWithNavController(navController)
//For log out button related stuff
nav_view.menu.findItem(R.id.nav_logout).setOnMenuItemClickListener {
Log.e("=>"," You clicked on logout !!! ")
true
}
}
override fun onSupportNavigateUp(): Boolean {
return findNavController(R.id.nav_host_fragment).navigateUp(appBarConfig)
|| super.onSupportNavigateUp()
}
}
You can also check out
my project from Git Hub
by this this link您还可以通过此链接从
Git Hub
check out
我的项目
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.