如何在程序中动态更改NavigationView的菜单?

6

你好,我想知道如何在NavigationView中更改菜单ID。在这种情况下,当用户单击RadioButton时,它会触发。我已经完成了这部分,但我只需要知道如何更改菜单,以便在单击RadioButton时显示不同的选项。

private var radioAdm:RadioButton? = null
private var radioAtle:RadioButton? = null
private var atletanav:BottomNavigationView? = null
private var menuopt = "@menu/admin_menu"

override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
     // Here is where I need to put that code
    }

我已经尝试过这个,但没有成功:

atleta_navbar.set(menu){menu=menuopt}
atleta_navbar.setTag(menuopt)

在NavigationView中,它将从这个状态转变为:

enter image description here

到这个:

enter image description here

2个回答

5
在Java中,您可以尝试以下方法在运行时重新填充NavigationView。
navigationView.getMenu().clear(); //clear old inflated items.
navigationView.inflateMenu(R.menu.new_navigation_drawer_items);

我不懂 Kotlin,但思路相同。


0

这段代码按照navylover的回答对我很有用。 1- 确保你有不同的菜单,在我的情况下,我有R.menu.navigationR.menu.navigation_normal

  private fun User() {
                // clear previous menu
                binding.navi.menu.clear()

               // add new menu item
                binding.navi.inflateMenu(R.menu.navigation)

              //add listener for new bottom naviagtion 

       binding.navi.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)

                 // when using Navigation component
                //set up bottom navigation bar with navigation graph 
                NavigationUI.setupWithNavController(binding.navi, navController)
            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接