Set selected item in Android BottomNavigationView

前端 未结 21 2361
谎友^
谎友^ 2020-11-27 14:16

I am using the new android.support.design.widget.BottomNavigationView from the support library. How can I set the current selection from code? I realized, that

21条回答
  •  隐瞒了意图╮
    2020-11-27 14:53

    This method work for me.

    private fun selectBottomNavigationViewMenuItem(bottomNavigationView : BottomNavigationView,@IdRes menuItemId: Int) {
                bottomNavigationView.setOnNavigationItemSelectedListener(null)
                bottomNavigationView.selectedItemId = menuItemId
                bottomNavigationView.setOnNavigationItemSelectedListener(this)
            }
    

    Example

     override fun onBackPressed() {
            replaceFragment(HomeFragment())
            selectBottomNavigationViewMenuItem(navView, R.id.navigation_home)
        }
    
    
    
    private fun replaceFragment(fragment: Fragment) {
            val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
            transaction.replace(R.id.frame_container, fragment)
            transaction.commit()
        }
    

提交回复
热议问题