如何以编程方式更改NavigationView的菜单? [英] How to change a Menu of a NavigationView programmatically?

查看:90
本文介绍了如何以编程方式更改NavigationView的菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想知道如何从NavigationView中更改菜单ID,在这种情况下,当用户单击RadioButton时会触发该菜单ID.我已经有了那部分,但是我只需要知道如何更改菜单,因此在菜单中,单击RadioButton时它将显示不同的选项.

Hello I'm wondering how can I change a Menu ID from a NavigationView, in this case it is triggered when the user clicks on a RadioButton. I have that part already but I just need to know how to change the Menu, so in the menu it will display different options when RadioButton is clicked.

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
    }

我已经尝试过了,但是没有成功:

I've tried this already but no success:

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

因此,在NavigationView中,它将从此开始:

So in the NavigationView it will go from this:

对此:

推荐答案

此代码对我有用,如navylover答案所解释 1-确保在我的情况下您有不同的菜单我有R.menu.navigationR.menu.navigation_normal

this code work for me as explain by navylover answer 1- make sure you have different menu in my case i have R.menu.navigation and R.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)
            }

这篇关于如何以编程方式更改NavigationView的菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆