Android BottomNavigationView,如何对一个项调用函数,并对其他项使用AppBarConfiguration?
这是配置Android BottomNavigationView,如何对一个项调用函数,并对其他项使用AppBarConfiguration?,android,kotlin,bottomnavigationview,android-bottomnavigationview,Android,Kotlin,Bottomnavigationview,Android Bottomnavigationview,这是配置BottomNavigationView导航的正常方式: val appBarConfiguration = AppBarConfiguration(setOf(R.id.navigation_home, R.id.navigation_months, R.id.navigation_due_date_calculator)) setupActionBarWithNavController(navController, appBarConfiguration) n
BottomNavigationView
导航的正常方式:
val appBarConfiguration = AppBarConfiguration(setOf(R.id.navigation_home, R.id.navigation_months, R.id.navigation_due_date_calculator))
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
但是,如果一个按钮想要调用一个函数而不是打开一个片段,会发生什么呢
我试过这个:
navView.setOnNavigationItemSelectedListener {
when (it.itemId){
R.id.navigation_other_useful_apps -> {
Toast.makeText(applicationContext, "Other Useful Apps", Toast.LENGTH_SHORT).show()
true
}
else -> true
}
}
问题是,当我添加该方法时,导航不再工作。如何对一个项目调用函数,对其他项目使用
AppBarConfiguration
。您必须覆盖OnOptions ItemSelected
函数,它将调用函数而不是打开片段
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.navigation_other_useful_apps -> {
Toast.makeText(applicationContext, "Other Useful Apps", Toast.LENGTH_SHORT).show()
return true;
}
}
return super.onOptionsItemSelected(item)
}
或者试试这个
navView.menu.findItem(R.id.navigation_other_useful_apps )
.setOnMenuItemClickListener { menuItem: MenuItem? ->
Toast.makeText(applicationContext, "Other Useful Apps", Toast.LENGTH_SHORT).show()
true
}