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

huangapple go评论78阅读模式
英文:

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&lt;BottomNavigationView&gt;(R.id.navigation_bar)
        navigationBar.setOnNavigationItemSelectedListener(navigation_bar)

    }
private val navigation_bar = BottomNavigationView.OnNavigationItemSelectedListener { item -&gt;

    when (item.itemId) {
        R.id.nav_profile -&gt; {
//            startActivity(Intent(this@ProfileActivity, ProfileActivity::class.java))
            return@OnNavigationItemSelectedListener true
        }

        R.id.nav_explore -&gt; {
            startActivity(Intent(this@ProfileActivity, ExploreActivity::class.java))
           return@OnNavigationItemSelectedListener true
        }

        R.id.nav_store -&gt; {
            startActivity(Intent(this@ProfileActivity, StoreActivity::class.java))
            return@OnNavigationItemSelectedListener true
        }

        R.id.nav_board -&gt; {
            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)

huangapple
  • 本文由 发表于 2020年7月23日 22:18:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63056438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定