下面列出了android.view.MenuItem#hasSubMenu ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public int getItemViewType(int position) {
MenuItem item = getItem(position);
int viewCount = item.hasSubMenu() ? item.getSubMenu().size() : 1;
if (item.getItemId() == R.id.update_menu_id) {
return UPDATE_MENU_ITEM;
} else if (viewCount == 4) {
return FOUR_BUTTON_MENU_ITEM;
} else if (viewCount == 3) {
return THREE_BUTTON_MENU_ITEM;
} else if (viewCount == 2) {
return TITLE_BUTTON_MENU_ITEM;
}
return STANDARD_MENU_ITEM;
}
@Override
public int getItemViewType(int position) {
MenuItem item = getItem(position);
int viewCount = item.hasSubMenu() ? item.getSubMenu().size() : 1;
if (item.getItemId() == R.id.update_menu_id) {
return UPDATE_MENU_ITEM;
} else if (viewCount == 5) {
return FIVE_BUTTON_MENU_ITEM;
} else if (viewCount == 4) {
return FOUR_BUTTON_MENU_ITEM;
} else if (viewCount == 3) {
return THREE_BUTTON_MENU_ITEM;
} else if (viewCount == 2) {
return TITLE_BUTTON_MENU_ITEM;
}
return STANDARD_MENU_ITEM;
}
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
handler.removeCallbacks(radialMenuHint);
EventId eventId = EVENT_ID_UNTRACKED; // Not tracking performance of menu events.
pipeline.returnFeedback(
eventId, Feedback.vibration(R.array.view_clicked_pattern).sound(R.raw.tick));
boolean handled =
menuActionInterceptor != null && menuActionInterceptor.onInterceptMenuClick(menuItem);
if (!handled) {
handled = client != null && client.onMenuItemClicked(menuItem);
}
if (!handled && (menuItem == null)) {
pipeline.returnFeedback(
EVENT_ID_UNTRACKED, Feedback.part().setInterruptAllFeedback(true));
}
if ((menuItem != null) && menuItem.hasSubMenu()) {
playScaleForMenu(menuItem.getSubMenu());
}
return true;
}
@Override
public int getItemViewType(int position) {
MenuItem item = getItem(position);
int viewCount = item.hasSubMenu() ? item.getSubMenu().size() : 1;
if (item.getItemId() == R.id.update_menu_id) {
return UPDATE_MENU_ITEM;
} else if (viewCount == 5) {
return FIVE_BUTTON_MENU_ITEM;
} else if (viewCount == 4) {
return FOUR_BUTTON_MENU_ITEM;
} else if (viewCount == 3) {
return THREE_BUTTON_MENU_ITEM;
} else if (viewCount == 2) {
return TITLE_BUTTON_MENU_ITEM;
}
return STANDARD_MENU_ITEM;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(!item.hasSubMenu()) {
createListView(item.getItemId());
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (!item.hasSubMenu()) {
createListView(item.getItemId());
}
return super.onOptionsItemSelected(item);
}
/**
* Try to tint all {@link Menu}s {@link MenuItem}s with given color
*/
@SuppressWarnings("ConstantConditions")
public void tintMenuItems(final Menu menu, final boolean recurse, @ColorInt final int iconColor) {
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
try {
tintDrawable(item.getIcon(), iconColor);
if (item.hasSubMenu() && recurse) {
tintMenuItems(item.getSubMenu(), recurse, iconColor);
}
} catch (Exception ignored) {
// This should not happen at all, but may in bad menu.xml configuration
}
}
}
/**
* Try to tint all {@link Menu}s {@link MenuItem}s with given color
*/
@SuppressWarnings("ConstantConditions")
public void tintMenuItems(final Menu menu, final boolean recurse, @ColorInt final int iconColor) {
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
try {
tintDrawable(item.getIcon(), iconColor);
if (item.hasSubMenu() && recurse) {
tintMenuItems(item.getSubMenu(), recurse, iconColor);
}
} catch (Exception ignored) {
// This should not happen at all, but may in bad menu.xml configuration
}
}
}
/**
* Try to tint all {@link Menu}s {@link MenuItem}s with given color
*/
@SuppressWarnings("ConstantConditions")
public void tintMenuItems(Menu menu, boolean recurse, @ColorInt int iconColor) {
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
tintDrawable(item.getIcon(), iconColor);
if (item.hasSubMenu() && recurse) {
tintMenuItems(item.getSubMenu(), recurse, iconColor);
}
}
}
/**
* Set the actions using a menu xml resource. There are 3 requirements of the menu xml:
* 1. The menu cannot have more than 5 items,
* 2. Each menu item cannot have a submenu, and
* 3. Each menu item must have an icon, title, and an id
*
* @param menuRes The resource id of the menu
* @param callback A callback to run on the main thread when an action is selected
* @return the BubbleActions instance that called this method
*/
public BubbleActions fromMenu(int menuRes, final MenuCallback callback) {
Menu menu = new PopupMenu(root.getContext(), null).getMenu();
MenuInflater inflater = new MenuInflater(root.getContext());
inflater.inflate(menuRes, menu);
if (menu.size() > BubbleActionOverlay.MAX_ACTIONS) {
throw new IllegalArgumentException(TAG + ": menu resource cannot have more than "
+ BubbleActionOverlay.MAX_ACTIONS + "actions.");
}
for (int i = 0; i < menu.size(); i++) {
final MenuItem item = menu.getItem(i);
if (item.hasSubMenu() || item.getIcon() == null || item.getTitle() == null || item.getItemId() == 0) {
throw new IllegalArgumentException(TAG + ": menu resource cannot have a submenu and " +
"must have an icon, title, and id.");
}
final int id = item.getItemId();
addAction(item.getTitle(), item.getIcon(), new Callback() {
@Override
public void doAction() {
callback.doAction(id);
}
});
}
return this;
}
/**
* Try to tint all {@link Menu}s {@link MenuItem}s with given color
*/
@SuppressWarnings("ConstantConditions")
public void tintMenuItems(Menu menu, boolean recurse, @ColorInt int iconColor) {
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
try {
tintDrawable(item.getIcon(), iconColor);
if (item.hasSubMenu() && recurse) {
tintMenuItems(item.getSubMenu(), recurse, iconColor);
}
} catch (Exception ignored) {
// This should not happen at all, but may in bad menu.xml configuration
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// For "Title only": Examples of matching an ID with one assigned in
// the XML
case R.id.jump:
Toast.makeText(this, "Jump up in the air!", Toast.LENGTH_SHORT).show();
return true;
case R.id.dive:
Toast.makeText(this, "Dive into the water!", Toast.LENGTH_SHORT).show();
return true;
// For "Groups": Toggle visibility of grouped menu items with
// nongrouped menu items
case R.id.browser_visibility:
// The refresh item is part of the browser group
final boolean shouldShowBrowser = !mMenu.findItem(R.id.refresh).isVisible();
mMenu.setGroupVisible(R.id.browser, shouldShowBrowser);
break;
case R.id.email_visibility:
// The reply item is part of the email group
final boolean shouldShowEmail = !mMenu.findItem(R.id.reply).isVisible();
mMenu.setGroupVisible(R.id.email, shouldShowEmail);
break;
// Generic catch all for all the other menu resources
default:
// Don't toast text when a submenu is clicked
if (!item.hasSubMenu()) {
Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
break;
}
return false;
}
boolean shouldAddChooserIntent(MenuItem item) {
return !item.hasSubMenu();
}
private List<BottomSheetItem> createAdapterItems(int dividerBackground, int titleTextColor,
int itemTextColor, int itemBackground,
int tintColor) {
List<BottomSheetItem> items = new ArrayList<>();
mTitles = 0;
boolean addedSubMenu = false;
for (int i = 0; i < mMenu.size(); i++) {
MenuItem item = mMenu.getItem(i);
if (item.isVisible()) {
if (item.hasSubMenu()) {
SubMenu subMenu = item.getSubMenu();
if (i != 0 && addedSubMenu) {
if (mMode == BottomSheetBuilder.MODE_GRID) {
throw new IllegalArgumentException("MODE_GRID can't have submenus." +
" Use MODE_LIST instead");
}
items.add(new BottomSheetDivider(dividerBackground));
}
CharSequence title = item.getTitle();
if (title != null && !title.equals("")) {
items.add(new BottomSheetHeader(title.toString(), titleTextColor));
mTitles++;
}
for (int j = 0; j < subMenu.size(); j++) {
MenuItem subItem = subMenu.getItem(j);
if (subItem.isVisible()) {
items.add(new BottomSheetMenuItem(subItem, itemTextColor,
itemBackground, tintColor));
addedSubMenu = true;
}
}
} else {
items.add(new BottomSheetMenuItem(item, itemTextColor, itemBackground, tintColor));
}
}
}
return items;
}
private List<BottomSheetItem> createAdapterItems(int dividerBackground, int titleTextColor,
int itemTextColor, int itemBackground,
int tintColor) {
List<BottomSheetItem> items = new ArrayList<>();
mTitles = 0;
boolean addedSubMenu = false;
for (int i = 0; i < mMenu.size(); i++) {
MenuItem item = mMenu.getItem(i);
if (item.isVisible()) {
if (item.hasSubMenu()) {
SubMenu subMenu = item.getSubMenu();
if (i != 0 && addedSubMenu) {
if (mMode == BottomSheetBuilder.MODE_GRID) {
throw new IllegalArgumentException("MODE_GRID can't have submenus." +
" Use MODE_LIST instead");
}
items.add(new BottomSheetDivider(dividerBackground));
}
CharSequence title = item.getTitle();
if (title != null && !title.equals("")) {
items.add(new BottomSheetHeader(title.toString(), titleTextColor));
mTitles++;
}
for (int j = 0; j < subMenu.size(); j++) {
MenuItem subItem = subMenu.getItem(j);
if (subItem.isVisible()) {
items.add(new BottomSheetMenuItem(subItem, itemTextColor,
itemBackground, tintColor));
addedSubMenu = true;
}
}
} else {
items.add(new BottomSheetMenuItem(item, itemTextColor, itemBackground, tintColor));
}
}
}
return items;
}
/**
* Flattens the visible menu items of {@link #menu} into {@link #items},
* while inserting separators between items when necessary.
*
* Adapted from the Design support library's NavigationMenuPresenter implementation
*/
private void prepareMenuItems() {
items.clear();
int currentGroupId = 0;
// Iterate over the menu items
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (item.isVisible()) {
if (item.hasSubMenu()) {
// Flatten the submenu
SubMenu subMenu = item.getSubMenu();
if (subMenu.hasVisibleItems()) {
if (menuType == LIST) {
items.add(SheetMenuItem.SEPARATOR);
// Add a header item if it has text
if (!TextUtils.isEmpty(item.getTitle())) {
items.add(SheetMenuItem.of(item));
}
}
// Add the sub-items
for (int subI = 0, size = subMenu.size(); subI < size; subI++) {
MenuItem subMenuItem = subMenu.getItem(subI);
if (subMenuItem.isVisible()) {
items.add(SheetMenuItem.of(subMenuItem));
}
}
// Add one more separator to the end to close it off if we have more items
if (menuType == LIST && i != menu.size() - 1) {
items.add(SheetMenuItem.SEPARATOR);
}
}
} else {
int groupId = item.getGroupId();
if (groupId != currentGroupId && menuType == LIST) {
items.add(SheetMenuItem.SEPARATOR);
}
items.add(SheetMenuItem.of(item));
currentGroupId = groupId;
}
}
}
}
boolean a(MenuItem menuitem)
{
return !menuitem.hasSubMenu();
}
boolean shouldAddChooserIntent(MenuItem item) {
return !item.hasSubMenu();
}
/**
* Flattens the visible menu items of {@link #menu} into {@link #items},
* while inserting separators between items when necessary.
*
* Adapted from the Design support library's NavigationMenuPresenter implementation
*/
private void prepareMenuItems() {
items.clear();
int currentGroupId = 0;
// Iterate over the menu items
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (item.isVisible()) {
if (item.hasSubMenu()) {
// Flatten the submenu
SubMenu subMenu = item.getSubMenu();
if (subMenu.hasVisibleItems()) {
if (menuType == LIST) {
items.add(SheetMenuItem.SEPARATOR);
// Add a header item if it has text
if (!TextUtils.isEmpty(item.getTitle())) {
items.add(SheetMenuItem.of(item));
}
}
// Add the sub-items
for (int subI = 0, size = subMenu.size(); subI < size; subI++) {
MenuItem subMenuItem = subMenu.getItem(subI);
if (subMenuItem.isVisible()) {
items.add(SheetMenuItem.of(subMenuItem));
}
}
// Add one more separator to the end to close it off if we have more items
if (menuType == LIST && i != menu.size() - 1) {
items.add(SheetMenuItem.SEPARATOR);
}
}
} else {
int groupId = item.getGroupId();
if (groupId != currentGroupId && menuType == LIST) {
items.add(SheetMenuItem.SEPARATOR);
}
items.add(SheetMenuItem.of(item));
currentGroupId = groupId;
}
}
}
}
boolean shouldAddChooserIntent(MenuItem item) {
return !item.hasSubMenu();
}