片段中不适当的上下文菜单

时间:2013-12-29 12:21:48

标签: android android-fragments navigation-drawer android-contextmenu

我有一个带有导航抽屉和片段的活动。他们每个人都有自己的ListView和一个上下文菜单。

导航抽屉内的上下文菜单工作正常。

问题在于片段内的菜单。当我进入片段的上下文菜单时,会显示两个导航抽屉菜单和片段的菜单。

活动的方法

@Override
protected void onCreate(Bundle savedInstanceState) {
    registerForContextMenu(mDrawerList); // mDrawrList is the navigation drawer's ListView
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch(item.getItemId()) {
    case R.id.action_edit:
        // Do some stuff
        return true;
    case R.id.action_delete:
        // Do some stuff
        return true;
    }
    return super.onContextItemSelected(item);
}

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

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.drawer, menu);
}

片段的方法

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    mListView = (ListView) view.findViewById(R.id.list);
    registerForContextMenu(mListView);
    // ...
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch(item.getItemId()) {
    case R.id.action_edit:
        // Do some stuff
        return true;
    case R.id.action_delete:
        // Do some stuff
        return true;
    }
    return super.onContextItemSelected(item);
}

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

    getActivity().getMenuInflater().inflate(R.menu.fragment_menu, menu);
}

提前谢谢。

2 个答案:

答案 0 :(得分:12)

<强>解决。

问题是活动的onCreateContextMenu被调用了。

我刚刚从片段的super.onCreateContextMenu(menu, v, menuInfo);方法中删除了onCreateContextMenu

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {

    getActivity().getMenuInflater().inflate(R.menu.fragment_menu, menu);
}

希望它能帮到某人。

答案 1 :(得分:0)

谢谢丹尼斯的验证码。

您不需要在片段中使用它

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch(item.getItemId()) {
    case R.id.action_edit:
        // Do some stuff
        return true;
    case R.id.action_delete:
        // Do some stuff
        return true;
    }
    return super.onContextItemSelected(item);
}