Android OnCreateContextMenu 和 ListView 项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12389184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OnCreateContextMenu and ListView items
提问by Eyal Biran
I have a LisView with several items. To this I've connected an OnItemClickListener (as an inner class), like this:
我有一个包含多个项目的 LisView。为此,我连接了一个 OnItemClickListener(作为内部类),如下所示:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ShoppingListApp02Activity.this, "List item selected:" +
items.get(position).getId(), Toast.LENGTH_LONG).show();
}
});
As is obvious, selecting an entriy displays elements of the object of that entry, in this example the selected Item object's ID (not the list ID, but the objects ID, set when creating the ArrayList items). This works nicely, and enables me to do anything I want with the selected item(s).
很明显,选择一个条目会显示该条目的对象的元素,在这个例子中是选定的 Item 对象的 ID(不是列表 ID,而是对象 ID,在创建 ArrayList 项目时设置)。这很好用,并使我能够对所选项目做任何我想做的事情。
Now I'd like to also have a "long-click" listener her, which opens a context menu for the selected ListView item. How do I do that? I've been able to attach an onCreateContextMenu listener to the ListView, but I don't see how I can get the elements of the ArrayList as with the onItemClickListener?
现在我还想有一个“长按”侦听器,它会打开所选 ListView 项目的上下文菜单。我怎么做?我已经能够将 onCreateContextMenu 侦听器附加到 ListView,但我不知道如何像 onItemClickListener 一样获取 ArrayList 的元素?
Here's what I've got:
这是我所拥有的:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(0, v.getId(), 0, "Something");
menu.add(0, v.getId(), 0, "Something else");
}
Since OnCreateConextMenu takes different parameters than the OnItemClickListener, how to I access the ArrayList's elements like in the OnItemClickListener?
由于 OnCreateConextMenu 采用与 OnItemClickListener 不同的参数,我如何像在 OnItemClickListener 中一样访问 ArrayList 的元素?
回答by Eyal Biran
If you decide you still want to use the context menu paradigm:
如果您决定仍要使用上下文菜单范例:
Consider this for working with lists:
考虑使用列表:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// Get the list
ListView list = (ListView)v;
// Get the list item position
AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
int position = info.position
// Now you can do whatever.. (Example, load different menus for different items)
list.getItem(position);
...
}
回答by waqaslam
Instead of messing with context menus (which are used in a wide context - like right-click in PC), ListView
offers onItemLongClickevent which is a lot more easier to implement. For example:
而不是搞乱上下文菜单(在广泛的上下文中使用 - 就像在 PC 中右键单击),ListView
提供了更容易实现的onItemLongClick事件。例如:
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
return false;
}
});
This will help you to achieve long-pressed actions on a row.
这将帮助您实现连续长按的操作。
回答by Gopinath
Open the context menu of the view within the event handler for the long press event on the row view.
在行视图上的长按事件的事件处理程序中打开视图的上下文菜单。
convertView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
((Activity)mContext).openContextMenu(v);
return true;
}
});
This way both the click on the view and the long press context menu works on the listview row item.
这样,单击视图和长按上下文菜单都适用于列表视图行项目。
回答by Rushabh Patel
First register context menu into your listview to open context menu:
首先将上下文菜单注册到您的列表视图中以打开上下文菜单:
registerForContextMenu(YOUR LIST-VIEW OBJECT);
then you can use your onCreateContextMenu() method:
然后你可以使用你的 onCreateContextMenu() 方法:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(0, v.getId(), 0, "Something");
menu.add(0, v.getId(), 0, "Something else");
}
You actually dont need to use longClickListener with your listview to use ContextMenu.
你实际上不需要在你的列表视图中使用 longClickListener 来使用 ContextMenu。
Hope it will help you.
希望它会帮助你。
回答by mohit
I will let you go through the below written example and see how it is implemented by using onContextItemSelected()
我会让你通过下面的书面例子,看看它是如何使用 onContextItemSelected() 实现的
public void onCreateContextMenu(ContextMenu menu,
View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(title);
menu.add(0, CMD_EDIT, 0, R.string.context_menu_edit);
menu.add(0, CMD_DELETE, 0, R.string.context_menu_delete);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case CMD_EDIT:
any_function();//add your functionality here i.e. what you want to do
return true;
case CMD_DELETE:
**confirmDelete**();
return true;
default:
return super.onContextItemSelected(item);
}
}
Hope this helps...
希望这可以帮助...
回答by Sergey Bondarenko
Try this for a View item in recycleView
为 recycleView 中的 View 项目试试这个
viewForContextMenu.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add("Context Menu Item1").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
//What should happent on selecting context menu item 1
return true;
}
});
}
});
You can use it with setting data to a ViewHolder item
您可以将数据设置为 ViewHolder 项目