我正在使用带有ActionBarSherlock的NavigationDrawer。当我点击菜单项onMenuItemClick()
时,方法不会被调用。
代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
return super.onPrepareOptionsMenu(menu);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
setTitle(AndroidUtils.getTitle());
SubMenu subMenu1 = menu.addSubMenu(null);
subMenu1.add(getString(R.string.save_loc)).setIcon(android.R.drawable.ic_menu_save).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
...
}
});
subMenu1.add(getString(R.string.save_em)).setIcon(android.R.drawable.ic_dialog_email).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
...
}
});
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(android.R.drawable.ic_menu_share);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onCreateOptionsMenu(menu);
}
答案 0 :(得分:0)
由于您已经在使用onOptionsItemSelected,您是否考虑过处理所有菜单项?像这样:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
setTitle(AndroidUtils.getTitle());
SubMenu subMenu1 = menu.addSubMenu(null);
subMenu1.add(0, R.id.menu_save_loc, 0, R.string.save_loc)
.setIcon(android.R.drawable.ic_menu_save);
subMenu1.add(0, R.id.menu_save_em, 0, R.string.save_em)
.setIcon(android.R.drawable.ic_dialog_email);
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(android.R.drawable.ic_menu_share);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
}
else if (itemId == R.id.menu_save_loc) {
...
}
else if (itemId == R.id.menu_save_em) {
...
}
else {
return super.onOptionsItemSelected(item);
}
return true;
}
答案 1 :(得分:0)
出于某些奇怪的原因,我无法处理菜单项的单击,但是为每个菜单视图设置setOnClickListener很有帮助。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.media_process_menu, menu);
for (int i = 0; i < menu.size(); i++) {
menu.getItem(i).getActionView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.save) {
//Your action
handleSaveAction();
}
}
});
}
return super.onCreateOptionsMenu(menu);
}