我刚刚开始使用Android NavigationUI通过抽屉菜单实现片段导航。尽管它确实有点像魔术,但我有一个主要问题。也就是说,菜单中有一个logout
项目,该项目不应该导航到任何“片段”,而应该导航到“动作”。尽管我在这里找到了类似的问题,例如this,但我还没有找到明确的答案。以下是我当前用于设置导航的功能:
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)
}
如果您对我的处理方法有任何建议,可以与您分享,我们将不胜感激。
答案 0 :(得分:0)
您只需要添加以下提到的代码
nav_view.menu.findItem(R.id.nav_logout).setOnMenuItemClickListener {
Log.e("=>"," You clicked on logout !!! ")
true
}
这是我的完整代码
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()
}
}
您还可以通过此this link从check out
Git Hub
我的项目