我有一个带有导航抽屉和片段的活动。每个都有自己的ListView和上下文菜单。
导航抽屉里面的上下文菜单正常工作。
问题出现在片段内的菜单中。当我进入片段的上下文菜单时,同时显示了导航抽屉菜单和片段菜单。
Activity的方法
@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);
}
Fragment的方法
@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);
}
提前感谢您。