如何在底部导航中区分菜单的值。我有角色响应,每个角色都有菜单。从主持人我有回应“管理员”。然后在活动中,我有了根据响应显示菜单的方法。
但是,现在我有错误:
Caused by: java.lang.ClassCastException: android.support.design.internal.BottomNavigationItemView cannot be cast to android.support.design.widget.BottomNavigationView
模块化方法
以XML
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/menu"/><android.support.design.widget.BottomNavigationView
android:id="@+id/navigation_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/menu_user"/>
在演示者中
private val roleUser = "admin"
override fun loadUserInfo() {
roleUser.let {
view?.showMenu(it)
}
}
活动
override fun showMenu(role: String) {
this.roleMenu = role
if (role == "admin") {
navigation_user.visibility = View.GONE
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
val fragment = CategoryFragment()
addFragment(fragment)
} else {
navigation.visibility = View.GONE
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListenerUser)
val fragment = DashboardFragment()
addFragment(fragment)
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
if (roleMenu == "admin") {
menuInflater.inflate(R.menu.menu, menu)
} else {
menuInflater.inflate(R.menu.menu_user, menu)
}
return true
}
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_category -> {
inflateCategoryFragmentIntoMainActivity()
return@OnNavigationItemSelectedListener true
}
R.id.navigation_product -> {
inflateProductFragmentIntoMainActivity()
return@OnNavigationItemSelectedListener true
}
R.id.navigation_user -> {
inflateUserFragmentIntoMainActivity()
return@OnNavigationItemSelectedListener true
}
}
false
}
private val mOnNavigationItemSelectedListenerUser = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home -> {
val fragment = DashboardFragment()
addFragment(fragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_transaction -> {
val fragment = TransactionPagerFragment()
addFragment(fragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_account -> {
val fragment = AccountPagerFragment()
addFragment(fragment)
return@OnNavigationItemSelectedListener true
}
}
false
}
当我回复“ admin”时,我应该看到角色admin的导航。