你如何在 Android 上的 ListActivity 中实现上下文菜单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/433761/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:09:05  来源:igfitidea点击:

How do you implement context menu in a ListActivity on Android?

androiduser-interfacelong-click

提问by pupeno

How do you implement a context menu triggered by a long click or tap on a ListActivity that is using the built in layouts and a ListAdapter?

您如何实现由长按或点击使用内置布局和 ListAdapter 的 ListActivity 触发的上下文菜单?

回答by pupeno

On the onCreate method call registerForContextMenulike this:

在 onCreate 方法调用registerForContextMenu像这样:

registerForContextMenu(getListView());

and then populate the menu on onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo). The menuInfo argument can provide information about which item was long-clicked in this way:

然后在onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)上填充菜单。menuInfo 参数可以通过这种方式提供有关哪个项目被长按的信息:

AdapterView.AdapterContextMenuInfo info;
try {
    info = (AdapterView.AdapterContextMenuInfo) menuInfo;
} catch (ClassCastException e) {
    Log.e(TAG, "bad menuInfo", e);
    return;
}
long id = getListAdapter().getItemId(info.position);

and you add menu items in the usual way calling menu.add:

并以通常调用menu.add 的方式添加菜单项:

menu.add(0, MENU_ITEM_ID, 0, R.string.menu_string);

and when the user picks an option, onContextItemSelectedis called. Also onMenuItemSelectedand this fact is not explicitly explained in the documentation except to say that you use the other method to receive the calls from the context menu; just be aware, don't share ids.

当用户选择一个选项时,onContextItemSelected被调用。同样 onMenuItemSelected并且文档中没有明确解释这一事实,只是说您使用其他方法从上下文菜单接收调用;请注意,不要共享 ID。

On onContextItemSelected you can get ahold of the MenuInfo and thus the id of the item selected by calling getMenuInfo():

在 onContextItemSelected 上,您可以通过调用getMenuInfo()获取 MenuInfo 以及所选项目的 ID :

try {
    info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
} catch (ClassCastException e) {
    Log.e(TAG, "bad menuInfo", e);
    return false;
}
long id = getListAdapter().getItemId(info.position);

回答by Romain Guy

回答by Dhiral Pandya

listView = (ListView) findViewById(R.id.listpockets);
registerForContextMenu(listView);



public void onCreateContextMenu(android.view.ContextMenu menu, View v, android.view.ContextMenu.ContextMenuInfo menuInfo) {
    //AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
    menu.setHeaderTitle(getString(R.string.titleDelete));   
    menu.add(0, CommonUtil.CONTEXT_MENU__DELETE_ID, 0, getString(R.string.menuDelete));
};
@Override
public boolean onContextItemSelected(MenuItem item) {

    if(item.getItemId() == CommonUtil.CONTEXT_MENU__DELETE_ID)
    {
       AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
       long id = this.listView.getItemIdAtPosition(info.position);
       Log.d(TAG, "Item ID at POSITION:"+id);
    }
    else
    {
        return false;
    }
    return true;
}