Android Kotlin - Material 底部导航禁用 Shift 模式和选择菜单

Android Kotlin - Material Bottom Navigation Disable Shift Mode and Selete Menu

  1. 我想用 kotlin 禁用 Shift 模式,我使用了 material BottomNavigationView

     <com.google.android.material.bottomnavigation.BottomNavigationView
     android:id="@+id/bottom_navigation"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_gravity="bottom"
     app:itemIconSize="@dimen/iconSize"
     android:background="?android:attr/windowBackground"
     app:layout_constraintBottom_toBottomOf="parent"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintStart_toStartOf="parent"
     app:menu="@menu/btm_nav_menu"
     app:labelVisibilityMode="labeled"
     />
    
  2. 我想单击帐户图标并开始activity单击后退按钮时登录 homeFragment 将被选中而不是帐户图标被选中

    bottom_navigation.setOnNavigationItemSelectedListener {
         when(it.itemId) {
             R.id.homeFragment -> showNav(HomeFragment())
             R.id.accountFragment -> { bottom_navigation.menu.findItem(R.id.homeFragment).setCheckable(true)
                 startActivity(Intent(this, MainActivity2::class.java)) }
         }
         true
    }
    
    
    fun showNav(fragment: Fragment){ 
         supportFragmentManager.beginTransaction().apply {
               replace(R.id.fragment_container, fragment)
               commit()
         }
    }
    

回答2。使用false,底部导航不会select图标

bottom_navigation.setOnNavigationItemSelectedListener {
        when(it.itemId) {
            R.id.homeFragment -> {
                showNav(HomeFragment())
                true
            }
            R.id.accountFragment -> {
                startActivity(Intent(this, MainActivity2::class.java))
                false
            } else -> true
        }
    }