底部导航栏BottomNavigationView

底部导航栏BottomNavigationView,第1张

底部导航栏BottomNavigationView

项目中一直使用的是TabHost来做的底部导航栏,最近项目语言换成kotlin发现TabHost不推荐使用了,于是找到了BottomNavigationView+Fragment的方式,简单,便捷!

第一种方式:BottomNavigationView+FramLayout

1.依赖

implementation "com.google.android.material:material:${versions.material}"

2.布局文件


    
    

3.meun文件配置



    
    
    

4.Activity配置

class MainActivity : baseActivity(ActivityMainBinding::inflate) {
    private var lastIndex = 0
    private var mFragments = mutableListOf()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initData()
        initBottomNavivation()
    }

    fun initData(){
        mFragments = ArrayList();
        mFragments.add(HomeFragment())
        mFragments.add(MessageFragment())
        mFragments.add(MineFragment())
        setFragmentPostion(0)
    }

    fun initBottomNavivation(){
        viewBinding.bottomNavigationView.itemIconTintList = null  //设置图标默认颜色
        viewBinding.bottomNavigationView.itemBackground = null   //设置点击动背景
        viewBinding.bottomNavigationView.setonNavigationItemSelectedListener { item ->
            when (item.itemId) {
                R.id.menu_home -> {
                    setFragmentPostion(0)
                    return@setonNavigationItemSelectedListener true
                }
                R.id.menu_message -> {
                    setFragmentPostion(1)
                    return@setonNavigationItemSelectedListener true
                }
                R.id.menu_mine -> {
                    setFragmentPostion(2)
                    return@setonNavigationItemSelectedListener true
                }
            }
            false
        }
    }

    fun setFragmentPostion(postion:Int){
        val ft = supportFragmentManager.beginTransaction()
        val currentFragment = mFragments[postion]
        val lastFragment = mFragments[lastIndex]
        lastIndex = postion
        ft.hide(lastFragment)
        if(!currentFragment.isAdded){
            supportFragmentManager.beginTransaction().remove(currentFragment).commit()
            ft.add(R.id.ll_fragmentLayout,currentFragment)
        }
        ft.show(currentFragment)
        ft.commitAllowingStateLoss()
    }


    override fun getPageName(): String {
        return PageName.MAIN
    }

}

第二种方式:BottomNavigationView+fragment标签

1.依赖

def nav_version = "2.3.5"

  // Java language implementation
  implementation "androidx.navigation:navigation-fragment:$nav_version"
  implementation "androidx.navigation:navigation-ui:$nav_version"

  // Kotlin
  implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
  implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

  // Feature module Support
  implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

  // Testing Navigation
  androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"

  // Jetpack Compose Integration
  implementation "androidx.navigation:navigation-compose:2.4.0-beta02"

2.layout
 




    
    

3.navigation
 


    

    

    

4.Activity

class NavigationViewActivity:baseActivity(ActivityNavagationViewBinding::inflate) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding.navView.itemIconTintList = null
        viewBinding.navView.itemBackground = null
        val appBarConfiguration = AppBarConfiguration(setOf(R.id.menu_home,R.id.menu_message,R.id.menu_mine))
        val navController = findNavController(R.id.nav_host_fragment)
        setupActionBarWithNavController(navController,appBarConfiguration)
        viewBinding.navView.setupWithNavController(navController)

    }

    override fun getPageName(): String {
        return ""
    }
}

第二种方式在开发过程中遇见的问题

1.报错

no start destination defined via app:startDestination for com.xxx.xxx/mobile_navigation

解决方法:在navigation的根目录上加入startDestiantion = "@+id/menu_home"标签

2.报错

java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference

需要在AndroidManifest中给Activity添加一个带ActionBar的主题

3.navigation 中item的id需要和menu文件中的item的id保持一直,否则Fragment切换会出问题。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5682687.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存