How to cancel the creation of a context menu after onCreateContextMenu() has been called

人走茶凉 提交于 2019-12-22 20:48:14

问题


I have an activity that registers a list view for the creation of the context menu:

registerForContextMenu(getListView());

The problem is that a long click on some items should not present the context menu, because the items are disabled.

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
    super.onCreateContextMenu(menu, v, menuInfo);

    bool bDisplayMenu = isItemEnabled(((AdapterView.AdapterContextMenuInfo) menuInfo).position);

    if(bDisplayMenu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }
    else
    {
        // WHAT SHOUlD I DO HERE TO CANCEL THE CREATION OF THE CONTEXT MENU?
    }

}

I don't see any way to cancel the creation of the context menu once onCreateContextMenu() has been called.


回答1:


Well, it turned out that if I exit from onCreateContextMenu() immediately after calling super, then the context menu doesn't appear at all.

Not sure this is the way to go (I didn't see any documentation regarding this).

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
    super.onCreateContextMenu(menu, v, menuInfo);

    bool bDisplayMenu = isItemEnabled(((AdapterView.AdapterContextMenuInfo) menuInfo).position);

    if(bDisplayMenu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }
    else
    {
        return; // the context menu will not be displayed
    }

}


来源:https://stackoverflow.com/questions/12407722/how-to-cancel-the-creation-of-a-context-menu-after-oncreatecontextmenu-has-bee

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!