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

问题描述

  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()
         }
    }
    

解决方法

答案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
        }
    }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...