我有一个带有导航抽屉和片段的活动。他们每个人都有自己的带有上下文菜单的 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);
}
先感谢您。