如果我使用导航组件单击导航抽屉中的菜单,如何执行操作而不是移动到另一个目的地?
how to perform an action instead of moving to another destination if I click a menu in Navigation Drawer using Navigation Component?
-
android
-
android-navigation
-
android-architecture-components
-
android-jetpack
-
android-architecture-navigation
我正在尝试使用 Jetpack Navigation 组件。因此,当我单击导航抽屉中的菜单时,如果我想从一个目的地转到另一个目的地,导航组件将自动处理
但我想在菜单被点击时执行一个操作,例如,如果抽屉中的菜单被点击,我想显示一条消息。
以旧方式,即使用片段事务,我可以轻松地从 onNavigationItemSelected
进行检查,但我不再找到该方法
那么如何在导航组件中做到这一点?
我试过检查 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
}
}
这是我的 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)
}
}
您可以按如下方式处理菜单点击
navView.menu.findItem(R.id.logout)
.setOnMenuItemClickListener { menuItem: MenuItem? ->
// write your code here
true
}
您尝试过使用
bottom_nav_view.setOnNavigationItemSelectedListener {
if (destination.id == R.id.my_destination {
// show toast in here
// but it doesn't work
}
}
android
android-navigation
android-architecture-components
android-jetpack
android-architecture-navigation
我正在尝试使用 Jetpack Navigation 组件。因此,当我单击导航抽屉中的菜单时,如果我想从一个目的地转到另一个目的地,导航组件将自动处理
但我想在菜单被点击时执行一个操作,例如,如果抽屉中的菜单被点击,我想显示一条消息。
以旧方式,即使用片段事务,我可以轻松地从 onNavigationItemSelected
进行检查,但我不再找到该方法
那么如何在导航组件中做到这一点?
我试过检查 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
}
}
这是我的 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)
}
}
您可以按如下方式处理菜单点击
navView.menu.findItem(R.id.logout)
.setOnMenuItemClickListener { menuItem: MenuItem? ->
// write your code here
true
}
您尝试过使用
bottom_nav_view.setOnNavigationItemSelectedListener {
if (destination.id == R.id.my_destination {
// show toast in here
// but it doesn't work
}
}