如何以编程方式从Android的导航抽屉中删除子菜单? [英] How to programmaticallly remove submenu from navigation drawer in Android?

查看:79
本文介绍了如何以编程方式从Android的导航抽屉中删除子菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序.首先,让我告诉你我不是专业人士.我现在要做的是根据情况在菜单中添加子菜单.但是我需要经常在我的应用中执行此操作.但是我的问题是我第一次在菜单中添加了子菜单.

I am developing an Android app. Firstly, let me tell you that I am not professional. What I am doing now is I am adding submenu to menu depending on a condition. But I need to do it very often in my app. But my problem is I added a submenu to the menu as first time.

但是第二次当我根据条件更新菜单时,现有子菜单不会被删除,新的子菜单会附加到导航抽屉中.如何删除以编程方式添加到菜单的子菜单?为什么我的代码没有删除它?

But second time when I update menu depending on condition, existing submenu is not removed and new submenu is appended to navigation drawer. How can I remove submenu that is programmatically added to menu? Why my code is not removing it?

这是我的代码

public void updateAuthUI()
    {
        isLoggedIn = tempStorage.getBoolean(getResources().getString(R.string.pref_is_logged_in),false);
        Menu menu = leftDrawer.getMenu();
        menu.removeItem(getResources().getInteger(R.integer.logout_item_id));
        menu.removeItem(getResources().getInteger(R.integer.login_item_id));
        menu.removeItem(getResources().getInteger(R.integer.register_item_id));
        SubMenu authSubMenu = menu.addSubMenu("Auth");

        if(isLoggedIn)
        {
            authSubMenu.add(1,getResources().getInteger(R.integer.logout_item_id),99,"Sign out");
        }
        else{
            authSubMenu.add(1,getResources().getInteger(R.integer.register_item_id),97,"Register");
            authSubMenu.add(1,getResources().getInteger(R.integer.login_item_id),98,"Sign in").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    openLoginActivity();
                    item.setChecked(true);
                    return true;
                }
            });
        }
    }

这是我的问题的屏幕截图

Here is the screenshot of my problem

如您所见,身份验证"子菜单已添加,但未删除现有子菜单.

As you can see Auth submenu is appended without removing existing one.

推荐答案

尝试

authSubMenu.clear();

在您的第一个

authSubMenu.add();

我刚刚在使用第三方库的Android应用中使用了SubMenu.clear(),并且需要从操作栏中清除不需要的子菜单. (我实际上是想完全删除该子菜单,这是我可以找到的唯一方法.它似乎起作用.)

I just used SubMenu.clear() in an Android app where I was using a third-party library, and I needed to clear out an unwanted submenu from the action bar. (I actually wanted to remove the submenu completely, and this was the only way I could find to do it. It seemed to work.)

与您的情况不同,在这里,authSubMenu是您刚刚通过menu.addSubMenu("Auth")添加的菜单,因此您希望它为空.但是,正如您所看到的,它显然不是空的:而是 返回该标题的现有子菜单. (我找不到达到这种效果的文档;我只是按照您报告的结果进行操作.)

That's different from your situation, where authSubMenu is a menu you just added via menu.addSubMenu("Auth"), so you would expect it to be empty. But, as you've seen, it apparently isn't empty: rather, addSubMenu("Auth") returns the existing submenu of that title. (I can't find documentation to that effect; I'm just going by the results you've reported.)

如果确实如此,那么authSubMenu.clear()将从子菜单中删除所有现有项目.

If that really is the case, as it appears to be, then authSubMenu.clear() will remove all existing items from the submenu.

这篇关于如何以编程方式从Android的导航抽屉中删除子菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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