How to Set selected item in BottomNavigationView

前端 未结 12 1845
半阙折子戏
半阙折子戏 2020-12-25 11:48

I am trying to set default item on activity created but it isn\'t working? This is my code:

protected void onCreate(Bundle savedInstanceState) {
    super.o         


        
12条回答
  •  庸人自扰
    2020-12-25 12:04

    If what you want is that the clicked element is ignored on the view and is not returned as "selected" you can return false after the click is handled, in some cases and some designs you could need to open an activity instead of a fragment and this will make selected the bottom item after the activity is closed.

    private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_shipment -> {
                currentItem = TAB_INDEX_SHIPMENT
                val intent = Intent(this, BookShipmentActivity::class.java)
                startActivity(intent)
                return@OnNavigationItemSelectedListener false
            }
        }
        false
    }
    

提交回复
热议问题