隐藏 Android 上下文菜单项
又一个新手问题。我有一个应用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在
onCreateContextMenu
方法中,您需要获取可能想要隐藏的菜单项,并根据列表位置将它们设置为不可见。像这样的东西:
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:
这是
PopupMenu
的解决方案,以防万一有人像我一样寻找它。这里,我在PopupMenu
的menu
布局文件中有 3 个按钮,在某些情况下我删除了其中一些按钮:当您使用
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 inmenu
layout file forPopupMenu
and I remove some of them in some cases:When you use
PopupMenu
onPrepareOptionsPanel
is not called. So you have to get menu items byid
while creatingPopupMenu
and remove those, that should not be available in a particular case.如果为
ListView
打开上下文菜单,menuInfo
将包含一个AdapterContextMenuInfo
类型的对象,它为您提供有关列表中哪个项目的信息被点击。如果它是第一个或最后一个项目,您可以简单地从上下文菜单中删除相应的条目,但我不太确定如果没有留下任何条目会发生什么。If a context menu is opened for a
ListView
,menuInfo
will contain an object of typeAdapterContextMenuInfo
, 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.如果需要,您可以禁用特定项目。
You can Disable a particular item if you want to.