onContextMenuClosed用于扩展DialogFragment的类

时间:2012-12-04 01:33:39

标签: android android-dialogfragment android-contextmenu

我有一个扩展DialogFragment的类,在这里我展示了一个ListView。我想为此列表中的项目提供LongPress选项。所以我覆盖了onCreateContextMenuonContextItemSelected。我的上下文菜单显示正确的选项。

我遇到的问题是我无法在getDialog().dismiss()方法中拨打onContextItemSelected

从扩展DialogFragment的类的onContextItemSelected方法中关闭扩展DialogFragment的类的正确方法是什么?

/*
 * (non-Javadoc)
 * @see android.support.v4.app.Fragment#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
 */
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)
{
    if(view.getId() == listView.getId())
    {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        Industry industry = (Industry) listView.getItemAtPosition(info.position);

        menu.setHeaderTitle(industry.name);
        menu.add(Menu.NONE, USE_INDUSTRY, 0, USE_INDUSTRY_TEXT);
    }
}

/*
 * (non-Javadoc)
 * @see android.support.v4.app.Fragment#onContextItemSelected(android.view.MenuItem)
 */
@Override
public boolean onContextItemSelected(MenuItem item)
{
    if(item.getItemId() == USE_INDUSTRY)
    {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        Industry industry = (Industry) listView.getItemAtPosition(info.position);

        MyApplication.BUS.post(new IndustryEvent(industry.ID, -2));
        getDialog().dismiss();
    }

    return true;
}

1 个答案:

答案 0 :(得分:2)

我找到了答案,我的Dialog Fragment中没有调用onContextItemSelected()。这意味着getDialog().dismiss()没有被调用。

我能找到的唯一方法是为MenuItem设置onMenuItemClickListener并在那里注册点击。以下是我正在使用的最终代码。

/*
 * (non-Javadoc)
 * @see android.support.v4.app.Fragment#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
 */
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)
{
    if(view.getId() == listView.getId() && isIndustryLevel)
    {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        Industry industry = (Industry) listView.getItemAtPosition(info.position);

        menu.setHeaderTitle(industry.name);
        menu.add(Menu.NONE, USE_INDUSTRY, 0, USE_INDUSTRY_TEXT);
        menu.getItem(0).setOnMenuItemClickListener(new OnUseIndustryButtonClick());
    }
}

private class OnUseIndustryButtonClick implements MenuItem.OnMenuItemClickListener
{
    /*
     * (non-Javadoc)
     * @see android.view.MenuItem.OnMenuItemClickListener#onMenuItemClick(android.view.MenuItem)
     */
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        Industry industry = app.industries.get(info.position);

        MyApplication.BUS.post(new IndustryEvent(industry.ID, -2));
        getDialog().dismiss();
        return true;
    }       
}