其他分享
首页 > 其他分享> > BottomNavigationView动态添加MenuItem

BottomNavigationView动态添加MenuItem

作者:互联网

随着业务的发展,我们需要根据不同角色的权限判断显示对应的tab项,在此我选择用BottomNavigationView。

核心代码:


    private lateinit var navigation: BottomNavigationView
    private var homeFragment: HomeFragment? = null
    private var mineFragment: MineFragment? = null
    private var claimFragment: ClaimFragment? = null
    private var isFragment: Fragment? = null
    private val mVM by viewModels<MainVM>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        navigation = findViewById(R.id.bottom_navigation)
        initNavigate()
        initFragment(savedInstanceState)
    }

    private fun initNavigate() {
        navigation.menu.clear()
        navigation.itemIconTintList = null

        mVM.getBottomTab().forEach {
            navigation.menu.add(it.groupId, it.homeId, it.order, it.name)
            navigation.menu.findItem(it.homeId).apply {
                setIcon(it.drawableId)
                actionView = getImageButton()
            }
        }
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
    }

    private fun getImageButton(): ImageButton {
        val imageButton = ImageButton(this)
        imageButton.setOnLongClickListener {
            false
        }
        return imageButton
    }


    private fun initFragment(savedInstanceState: Bundle?) {
        //判断activity是否重建,如果不是,则不需要重新建立fragment.
        if (savedInstanceState == null) {
            val fm = supportFragmentManager
            val ft = fm.beginTransaction()
            if (homeFragment == null) {
                homeFragment = HomeFragment()
            }
            isFragment = homeFragment
            ft.replace(R.id.fl_container, homeFragment!!).commit()
        }
    }

    private val mOnNavigationItemSelectedListener =
        BottomNavigationView.OnNavigationItemSelectedListener { item ->
            when (item.itemId) {
                R.id.id_home -> {
                    if (homeFragment == null) {
                        homeFragment = HomeFragment()
                    }
                    switchContent(isFragment, homeFragment!!)
                    return@OnNavigationItemSelectedListener true
                }

                R.id.id_claim -> {
                    if (claimFragment == null) {
                        claimFragment = ClaimFragment()
                    }
                    switchContent(isFragment, claimFragment!!)
                    return@OnNavigationItemSelectedListener true
                }
                R.id.id_mine -> {
                    if (mineFragment == null) {
                        mineFragment = MineFragment()
                    }
                    switchContent(isFragment, mineFragment!!)
                    return@OnNavigationItemSelectedListener true
                }

            }
            false
        }

    private fun switchContent(from: Fragment?, to: Fragment) {
        if (isFragment !== to) {
            isFragment = to
            val fm: FragmentManager = supportFragmentManager
            //添加渐隐渐现的动画
            val ft: FragmentTransaction = fm.beginTransaction()
            if (!to.isAdded) {// 先判断是否被add过
                if (from != null) {
                    ft.hide(from).add(R.id.fl_container, to).commit()
                }
            } else {
                if (from != null) {
                    ft.hide(from).show(to).commit()
                }
            }
        }
    }


数据源:
 


    fun getBottomTab(): List<HomeMenuBean> {
        return listOf(
            HomeMenuBean(
                0, R.id.id_home, 0,
                R.drawable.nav_home_home,
                SysUtil.getString(R.string.home)
            ),
            HomeMenuBean(
                0, R.id.id_claim, 1,
                R.drawable.nav_home_claim,
                SysUtil.getString(R.string.claim)
            ),
            HomeMenuBean(
                0, R.id.id_mine, 2,
                R.drawable.nav_home_mine,
                SysUtil.getString(R.string.mine)
            )
        )
    }


实体类:
 


data class HomeMenuBean(
    val groupId: Int,
    val homeId: Int,
    val order: Int,
    val drawableId: Int,
    val name: String
)


Layout:
 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".main.MainActivity">

    <FrameLayout
        android:id="@+id/fl_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
        app:layout_constraintTop_toTopOf="parent" />


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:itemHorizontalTranslationEnabled="false"
        app:itemTextColor="@color/bottom_navigate_text_color"
        app:labelVisibilityMode="labeled"
        app:itemBackground="@null"
        app:layout_constraintBottom_toBottomOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

添加ids:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="id_home" type="id" />
    <item name="id_claim" type="id" />
    <item name="id_mine" type="id" />
</resources>


 

标签:val,navigation,private,homeFragment,添加,MenuItem,null,BottomNavigationView,id
来源: https://blog.csdn.net/xwk568571931/article/details/122099397