不添加机器人NavigationView子如果有一些项机器人、NavigationView

由网友(执念、)分享简介:嗯,我喜欢NavigationView的想法,但有不愉快的经历与编程添加菜单项和放大器;子菜单Well, i liked an idea of NavigationView, but had bad experience with programmatically adding menu items & submen...

嗯,我喜欢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;
}
阅读全文

相关推荐

最新文章