英文:
Bottom navigation icon only changing color when clicked twice?
问题
当我点击图标时,除非我再次点击它,它的颜色不会改变,它会转到相应的活动,但只有第一个图标保持高亮。我必须再次点击它以使图标改变颜色。我的代码有什么问题吗?
class ProfileActivity : BaseActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_profile)
val navigationBar = findViewById<BottomNavigationView>(R.id.navigation_bar)
navigationBar.setOnNavigationItemSelectedListener(navigation_bar)
}
private val navigation_bar = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.nav_profile -> {
// startActivity(Intent(this@ProfileActivity, ProfileActivity::class.java))
return@OnNavigationItemSelectedListener true
}
R.id.nav_explore -> {
startActivity(Intent(this@ProfileActivity, ExploreActivity::class.java))
return@OnNavigationItemSelectedListener true
}
R.id.nav_store -> {
startActivity(Intent(this@ProfileActivity, StoreActivity::class.java))
return@OnNavigationItemSelectedListener true
}
R.id.nav_board -> {
startActivity(Intent(this@ProfileActivity, BoardActivity::class.java))
return@OnNavigationItemSelectedListener true
}
}
false
} }
英文:
When I click on an icon it doesn't change color unless I click it again, it goes to the activity but only the first icon remains highlighted. I have to click it again for the icon to change color. What is wrong with my code?
class ProfileActivity : BaseActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_profile)
val navigationBar = findViewById<BottomNavigationView>(R.id.navigation_bar)
navigationBar.setOnNavigationItemSelectedListener(navigation_bar)
}
private val navigation_bar = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.nav_profile -> {
// startActivity(Intent(this@ProfileActivity, ProfileActivity::class.java))
return@OnNavigationItemSelectedListener true
}
R.id.nav_explore -> {
startActivity(Intent(this@ProfileActivity, ExploreActivity::class.java))
return@OnNavigationItemSelectedListener true
}
R.id.nav_store -> {
startActivity(Intent(this@ProfileActivity, StoreActivity::class.java))
return@OnNavigationItemSelectedListener true
}
R.id.nav_board -> {
startActivity(Intent(this@ProfileActivity, BoardActivity::class.java))
return@OnNavigationItemSelectedListener true
}
}
false
} }
答案1
得分: 1
你正在使用活动。请使用片段。我认为这将解决问题。
英文:
You are using activity. use Fragment. i think it will be solved.
答案2
得分: 1
你正在为每个项目使用不同的活动。
您需要在活动中处理所选项目,例如:
navigationBar.setSelectedItemId(R.id.nav_explore)
英文:
You are using different activities for each item.
You have to handle the selected items in your activities with something like:
navigationBar.setSelectedItemId(R.id.nav_explore)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论