不添加机器人NavigationView子如果有一些项 [英] android NavigationView submenus aren't added if there are some items

查看:172
本文介绍了不添加机器人NavigationView子如果有一些项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我喜欢NavigationView的想法,但有不愉快的经历与编程添加菜单项和放大器;子菜单

Well, i liked an idea of NavigationView, but had bad experience with programmatically adding menu items & submenus

protected void inflateMenu(Menu menu){
    menu.addSubMenu(1, 2, 0, "Heading");
    menu.add(MAIN_GROUP_ID, 0, 0, "Something 2");
    menu.add(MAIN_GROUP_ID, 1, 0, "Title");

    menu.addSubMenu(1, 2, 0, "Sub Menu");
    menu.add(1, 3, 0, "Menu #1");

}

子菜单没有出现在NavigationView,但如果我仍然只是增加子菜单,没有任何项目,他们出现在屏幕上,所以因此问题,如何解决这个问题?

Sub menus isn't appearing on NavigationView, but if i remain just adding sub menus without any items, they appeared on the screen, so hence the question, how to solve this?

推荐答案

嗯,我找到的解决方案,我们只需要使用 NavigationMenu presenter#updateMenuView(布尔)和人口菜单之后把它叫做

Well, i found solution, we just need access to NavigationMenuPresenter#updateMenuView(boolean), and call it right after population menu

/**
 * Tricky {@link NavigationView} doesn't allow create dynamic
 * menu, be clearly adding menu items on the fly doesn't updates
 * Menu Adapter in {@link NavigationView} so, to we should update
 * it manually view {@link NavigationMenuPresenter#updateMenuView(boolean)}
 *
 * Returns {@link NavigationMenuPresenter} instance from {@link NavigationView}
 */
static NavigationMenuPresenter getNavigationMenuPresenter(NavigationView view){
    try {
        Field presenterField = NavigationView.class.getDeclaredField("mPresenter");
        presenterField.setAccessible(true);
        return (NavigationMenuPresenter) presenterField.get(view);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
}

这篇关于不添加机器人NavigationView子如果有一些项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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