简体   繁体   English

底部导航图标仅在单击两次时更改颜色?

[英]Bottom navigation icon only changing color when clicked twice?

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
} }

You are using activity.您正在使用活动。 use Fragment.使用片段。 i think it will be solved.我认为它会得到解决。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击时更改底部导航图标 - Change bottom navigation icon when clicked 底部导航图标颜色变化 - Bottom Navigation Icon color change 工具栏中的导航图标颜色没有改变? - Navigation icon color in toolbar is not changing? 单击 JButton 时更改 JPanel 图标 - Changing JPanel Icon when JButton is clicked 如何在Android中更改屏幕底部的软导航背景和图标颜色? - How to change the bottom on screen soft navigation background and icon color in android? 在getListCellRendererComponent中双击时更改颜色 - Changing color when double clicked in the getListCellRendererComponent 为什么我无法使用渐变颜色 android 更改图标颜色和文本底部导航 - Why i can't change icon color and text bottom navigation with gradient color android 更改 JLabel 中图标的颜色 - Changing the color of an icon in a JLabel 在片段之间切换时底部导航栏向下推 - Bottom Navigation Bar pushes down when changing between fragments 通过单击一个片段中的按钮并更改android studio中的底部导航图标,从一个片段重定向到另一个片段 - redirection from one fragment to another by clicking a button in one fragment together with changing the bottom navigation icon in android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM