简体   繁体   English

如何动态添加 NavigationView 菜单?

[英]How to add NavigationView Menu dynamically?

So, I would like add menu items to navigationView.所以,我想向 navigationView 添加菜单项。 The idea is that, there are Projects, and every project belongs to a group.这个想法是,有项目,每个项目都属于一个组。 So the group count can be from 1 to N.所以组数可以从 1 到 N。

These groups are stored in Room database, thease groups don't change at run time, so it is not necessary to invalidate/reset navigation view.这些组存储在 Room 数据库中,这些组在运行时不会更改,因此没有必要使导航视图无效/重置。

I created a main fragment whic is called Projects, when the user clicks the generated project group menu items, I would like to navigate to the Projects fragment, but with different projectGroupId because of the filtering.我创建了一个名为 Projects 的主要片段,当用户单击生成的项目组菜单项时,我想导航到 Projects 片段,但由于过滤而具有不同的 projectGroupId。

Althought I found the solution for creating menu items, but I don't know how to handle the click event, and how to highlight the menu item like a standard static menu item.虽然我找到了创建菜单项的解决方案,但我不知道如何处理单击事件,以及如何像标准静态菜单项一样突出显示菜单项。

Main Activity navigation view
    private fun setUpNavigation() {
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        setSupportActionBar(binding.appBarMain.toolbar)

        val menuItems = mutableListOf(
            R.id.nav_home, R.id.nav_calendar, R.id.nav_project_browser
        )

        appBarConfiguration = AppBarConfiguration(
            menuItems.toSet(),
            binding.drawerLayout
        )

        setCounterValueToMenuItem(binding.navView.menu.findItem(R.id.nav_home),2)

        setupActionBarWithNavController(navController, appBarConfiguration)
        binding.navView.setupWithNavController(navController)
    }

Generate menu method
rivate fun addExtraMenu(){
        val navView = binding.navView
        val menu = navView.menu
        val subMenu = menu.addSubMenu("Custom menus")

        subMenu.add(0,0,0,"Menu 1",)
        setMenuItemActionView(subMenu[0])

        subMenu.add(0,1,0,"Menu2")

        subMenu[0].setOnMenuItemClickListener {
            it.isChecked = true

            return@setOnMenuItemClickListener true
        }

        setCounterValueToMenuItem(subMenu.getItem(0),2)
    }

    private fun setMenuItemActionView(menuItem: MenuItem){
        menuItem.setActionView(R.layout.drawer_menu_with_counter)
    }

    private fun setCounterValueToMenuItem(menuItem: MenuItem,count: Int){
        val frameLayout: FrameLayout = menuItem.actionView as FrameLayout
        val textView = frameLayout.findViewById<TextView>(R.id.counter)

        textView.text = count.toString()
    }

So finally I figured out how to handle this situation.所以最后我想出了如何处理这种情况。

In the menu items observer (in fact, these items are just a String name list), I call addExtraMenu function.在菜单项观察器中(其实这些项只是一个String名字列表),我调用了addExtraMenu函数。 The first menu item index starts from 0...N, therefore the submenu index has to be a big number, so that by chance it should not be the same.第一个菜单项索引从 0...N 开始,因此子菜单索引必须是一个大数字,以便偶然它不应该相同。 If the menuItems changed in runtime, you have to remove the old subMenu, and recreate it.如果 menuItems 在运行时更改,您必须删除旧的 subMenu,然后重新创建它。 I didn't want to handle the checked state, it wasn't necessary in my case.我不想处理检查状态,在我的情况下没有必要。

 private fun addExtraMenu(types: List<IndustryType>) {
        val navView = binding.navView
        val menu = navView.menu
        val subMenuIndex = 999

        menu.findItem(subMenuIndex)?.let {
            menu.removeItem(it.itemId)
        }

        val subMenu = menu.addSubMenu(0, subMenuIndex, 0, R.string.drawer_menu_dynamic_group_label)

        types.forEachIndexed { _, industryType ->

            subMenu.add(0, industryType.indexOfMenu, industryType.indexOfMenu, industryType.name)

            setMenuItemActionView(subMenu[industryType.indexOfMenu])

            subMenu.getItem(industryType.indexOfMenu)?.setOnMenuItemClickListener {
                setDynamicMenuClickListener(industryType.oid)
            }
        }
    }

    private fun setDynamicMenuClickListener(industryTypeOid: String?): Boolean {
        val navController = findNavController(R.id.nav_host_fragment_content_main)

        binding.drawerLayout.close()

        val b = bundleOf(Pair("industryTypeOid", industryTypeOid))

        navController.navigate(R.id.nav_project_browser, b)

        return true
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Navigationview 菜单中动态添加复选框? - How to add checkboxes in a Navigationview menu dynamically? 如何使用菜单布局将Custom项添加到NavigationView? - How to add Custom item to a NavigationView with a menu layout? 如何将项目添加到NavigationView中的菜单组 - How to add an item to a menu group in NavigationView 如何在NavigationView中动态添加自定义MenuItem? - How to add custom MenuItem dynamically in NavigationView? 如何在onClick期间动态地将android NavigationView与另一个菜单充气? - How to inflate android NavigationView with another menu dynamically during onClick? 如何在NavigationView android中添加N级可扩展菜单? - How to add N-level of Expandable menu in NavigationView android? 如何使用菜单布局将自定义项添加到NavigationView? - How can I add a custom item to a NavigationView with a menu layout? 如何以编程方式而不是菜单xml将子菜单项添加到NavigationView - How to add submenu items to NavigationView programmatically instead of menu xml 我如何将选项菜单添加到 NavigationView 的片段 - How can i add options menu to fragment from NavigationView 如何在 NavigationView 中添加 Listview? - How to add Listview in NavigationView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM