Android NavigationView 阻止选择项目,setCheckedItem 在 onNavigationItemSelected 中不起作用
Android NavigationView prevent selecting item, setCheckedItem not working inside onNavigationItemSelected
这是onNavigationItemSelected
中的代码
R.id.nav_subscribed -> {
if (prefs.getLong("userid", 0L) == 0L) {
when (currentGal) {
"top" -> {
// navView.setCheckedItem(R.id.nav_top)
// navView.menu.getItem(R.id.nav_top).isChecked = true
}
}
val i = Intent(this, Login::class.java)
this.startActivity(i)
} else {
if(currentGal != "subscriptions"){
getMemes("subscriptions", 0)
}
}
}
我试图阻止 nav_subscribed
在用户未登录时被检查 check/keep 被检查 nav_top
navView.setCheckedItem(R.id.nav_top)
不工作并且
navView.menu.getItem(R.id.nav_top).isChecked = true
使应用程序崩溃。那么该怎么做呢?顺便说一句 android 是糟糕的垃圾软件
navigationView?.setOnNavigationItemSelectedListener {
when (it.itemId) {
R.id.nav_subscribed -> {
// if you don't want an item to be selected return false
return@setOnNavigationItemSelectedListener false
}
R.id.nav_top -> {
}
}
//return true to select the other items
true
}
这是onNavigationItemSelected
R.id.nav_subscribed -> {
if (prefs.getLong("userid", 0L) == 0L) {
when (currentGal) {
"top" -> {
// navView.setCheckedItem(R.id.nav_top)
// navView.menu.getItem(R.id.nav_top).isChecked = true
}
}
val i = Intent(this, Login::class.java)
this.startActivity(i)
} else {
if(currentGal != "subscriptions"){
getMemes("subscriptions", 0)
}
}
}
我试图阻止 nav_subscribed
在用户未登录时被检查 check/keep 被检查 nav_top
navView.setCheckedItem(R.id.nav_top)
不工作并且
navView.menu.getItem(R.id.nav_top).isChecked = true
使应用程序崩溃。那么该怎么做呢?顺便说一句 android 是糟糕的垃圾软件
navigationView?.setOnNavigationItemSelectedListener {
when (it.itemId) {
R.id.nav_subscribed -> {
// if you don't want an item to be selected return false
return@setOnNavigationItemSelectedListener false
}
R.id.nav_top -> {
}
}
//return true to select the other items
true
}