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

时间:2020-08-13 07:20:27

标签: android kotlin bottomnavigationview android-bottomnav

  1. 我想用kotlin禁用Shift模式,我使用了材质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. 我要单击帐户图标并在单击后退按钮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()
         }
    }
    

1 个答案:

答案 0 :(得分:0)

答案2.使用false,底部导航不会选择图标

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