隐藏 Android 上下文菜单项

发布于 2024-10-03 08:20:36 字数 547 浏览 0 评论 0原文

又一个新手问题。我有一个应用于 ListView 的上下文菜单,它仅允许用户向上或向下移动项目,或删除该项目。

我在 onContextItemSelected() 中有代码来防止事物向上移动到列表的顶部或底部等,但我宁愿首先隐藏上下文菜单项,如果(例如)列表中的顶部项目是已选择。

我认为我需要在 onCreateContextMenu 中执行此操作,但我不确定如何执行。

这是我的 onCreateContextMenu 代码:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mycontextmenu, menu);
}

谢谢,
工作时间

Another newbie question. I have a context menu that I apply for a ListView that simply allows the user to move items up or down, or delete the item.

I have code in onContextItemSelected() to prevent things from moving up past top or bottom of the list, etc., but I'd rather hide the context menu items in the first place if (for instance) the top item in the list is selected.

I assume that I need to do this in onCreateContextMenu, but I'm not sure how.

Here is my onCreateContextMenu code:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mycontextmenu, menu);
}

Thanks,
wTs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

与风相奔跑 2024-10-10 08:20:36

onCreateContextMenu 方法中,您需要获取可能想要隐藏的菜单项,并根据列表位置将它们设置为不可见。

像这样的东西:

AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

if(info.position < 1) {
   myLocationMenuItem = menu.findItem(R.id.myLocation);
   myLocationMenuItem.setVisible(enable);
}

In your onCreateContextMenu method, you need to get the menu items that you potentially want to hide and set them as not visible based on the list positions.

Something like this:

AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

if(info.position < 1) {
   myLocationMenuItem = menu.findItem(R.id.myLocation);
   myLocationMenuItem.setVisible(enable);
}
南笙 2024-10-10 08:20:36

这是 PopupMenu 的解决方案,以防万一有人像我一样寻找它。这里,我在 PopupMenumenu 布局文件中有 3 个按钮,在某些情况下我删除了其中一些按钮:

    PopupMenu popup = new PopupMenu(this, this.actionButton);
    popup.setOnMenuItemClickListener(this);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu_resume_view, popup.getMenu());

    Menu menu = popup.getMenu();
    if (this.resume.isPublished()) {
        menu.removeItem(R.id.menu_button_publish);
    }
    else {
        menu.removeItem(R.id.menu_button_unpublish);
        menu.removeItem(R.id.menu_button_update_publish_date);
    }

    popup.show();

当您使用 PopupMenu onPrepareOptionsPanel 时 未被调用。因此,您必须在创建 PopupMenu 时通过 id 获取菜单项并删除它们,这在特定情况下不应该可用。

This is a solution for a PopupMenu just in case somebody is seeking for it as I did. Here I have 3 buttons in menu layout file for PopupMenu and I remove some of them in some cases:

    PopupMenu popup = new PopupMenu(this, this.actionButton);
    popup.setOnMenuItemClickListener(this);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu_resume_view, popup.getMenu());

    Menu menu = popup.getMenu();
    if (this.resume.isPublished()) {
        menu.removeItem(R.id.menu_button_publish);
    }
    else {
        menu.removeItem(R.id.menu_button_unpublish);
        menu.removeItem(R.id.menu_button_update_publish_date);
    }

    popup.show();

When you use PopupMenu onPrepareOptionsPanel is not called. So you have to get menu items by id while creating PopupMenu and remove those, that should not be available in a particular case.

机场等船 2024-10-10 08:20:36

如果为 ListView 打开上下文菜单,menuInfo 将包含一个 AdapterContextMenuInfo 类型的对象,它为您提供有关列表中哪个项目的信息被点击。如果它是第一个或最后一个项目,您可以简单地从上下文菜单中删除相应的条目,但我不太确定如果没有留下任何条目会发生什么。

If a context menu is opened for a ListView, menuInfo will contain an object of type AdapterContextMenuInfo, which gives you information about which item in the list was clicked. If it is the first or the last item, you can simply remove the corresponding entries from the context menu, though I'm not quite sure what happens if no entries are left.

极致的悲 2024-10-10 08:20:36

如果需要,您可以禁用特定项目。

@Override
        public void onCreateContextMenu(ContextMenu menu, View v,
                ContextMenuInfo menuInfo)
     {
                AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

                int position = info.position;

                // use this position to decide your item clicked

                menu.clear();
                menu.setHeaderTitle("Context Menu Title");
                String[] menuItems = getResources().getStringArray(
                        R.array.menu_context);


                for (int i = 0; i < menuItems.length; i++)
                {
                    menu.add(Menu.NONE, i , i, menuItems[i]);
                }
               menu.getItem(0).setEnabled(**Conditional check**);
               menu.getItem(1).setEnabled(**Conditional check**);
               menu.getItem(2).setEnabled(**Conditional check**);
     }

You can Disable a particular item if you want to.

@Override
        public void onCreateContextMenu(ContextMenu menu, View v,
                ContextMenuInfo menuInfo)
     {
                AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

                int position = info.position;

                // use this position to decide your item clicked

                menu.clear();
                menu.setHeaderTitle("Context Menu Title");
                String[] menuItems = getResources().getStringArray(
                        R.array.menu_context);


                for (int i = 0; i < menuItems.length; i++)
                {
                    menu.add(Menu.NONE, i , i, menuItems[i]);
                }
               menu.getItem(0).setEnabled(**Conditional check**);
               menu.getItem(1).setEnabled(**Conditional check**);
               menu.getItem(2).setEnabled(**Conditional check**);
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文