diff options
author | 2015-11-16 22:24:29 +0000 | |
---|---|---|
committer | 2015-11-16 22:24:29 +0000 | |
commit | 1ca1860f973de351b2f0f6e6e924daf791a1e1a3 (patch) | |
tree | 7b9a67ac4d216c683a45f0a96d7b4e47a3eccdb5 | |
parent | 1a6436c9ea85a3a5c6b9e9ae3f1ca9a666453168 (diff) | |
parent | 7323810898bd4a63b67c3568c27d04206dd59a1c (diff) |
Merge "Clean up PopupMenu"
-rw-r--r-- | core/java/android/widget/PopupMenu.java | 147 |
1 files changed, 71 insertions, 76 deletions
diff --git a/core/java/android/widget/PopupMenu.java b/core/java/android/widget/PopupMenu.java index 34a843925700..a53df8835b08 100644 --- a/core/java/android/widget/PopupMenu.java +++ b/core/java/android/widget/PopupMenu.java @@ -21,7 +21,6 @@ import com.android.internal.view.menu.MenuBuilder; import com.android.internal.view.menu.MenuPopupHelper; import com.android.internal.view.menu.MenuPresenter; import com.android.internal.view.menu.ShowableListMenu; -import com.android.internal.view.menu.SubMenuBuilder; import android.annotation.MenuRes; import android.content.Context; @@ -33,35 +32,23 @@ import android.view.View; import android.view.View.OnTouchListener; /** - * A PopupMenu displays a {@link Menu} in a modal popup window anchored to a {@link View}. - * The popup will appear below the anchor view if there is room, or above it if there is not. - * If the IME is visible the popup will not overlap it until it is touched. Touching outside - * of the popup will dismiss it. + * A PopupMenu displays a {@link Menu} in a modal popup window anchored to a + * {@link View}. The popup will appear below the anchor view if there is room, + * or above it if there is not. If the IME is visible the popup will not + * overlap it until it is touched. Touching outside of the popup will dismiss + * it. */ -public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback { +public class PopupMenu { private final Context mContext; private final MenuBuilder mMenu; private final View mAnchor; private final MenuPopupHelper mPopup; - private final boolean mShowCascadingMenus; private OnMenuItemClickListener mMenuItemClickListener; private OnDismissListener mDismissListener; private OnTouchListener mDragListener; /** - * Callback interface used to notify the application that the menu has closed. - */ - public interface OnDismissListener { - /** - * Called when the associated menu has been dismissed. - * - * @param menu The PopupMenu that was dismissed. - */ - public void onDismiss(PopupMenu menu); - } - - /** * Constructor to create a new popup menu with an anchor view. * * @param context Context the popup menu is running in, through which it @@ -108,14 +95,40 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback { public PopupMenu(Context context, View anchor, int gravity, int popupStyleAttr, int popupStyleRes) { mContext = context; - mShowCascadingMenus = context.getResources().getBoolean( - com.android.internal.R.bool.config_enableCascadingSubmenus); - mMenu = new MenuBuilder(context); - mMenu.setCallback(this); mAnchor = anchor; + + mMenu = new MenuBuilder(context); + mMenu.setCallback(new MenuBuilder.Callback() { + @Override + public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { + if (mMenuItemClickListener != null) { + return mMenuItemClickListener.onMenuItemClick(item); + } + return false; + } + + @Override + public void onMenuModeChange(MenuBuilder menu) { + } + }); + mPopup = new MenuPopupHelper(context, mMenu, anchor, false, popupStyleAttr, popupStyleRes); mPopup.setGravity(gravity); - mPopup.setCallback(this); + mPopup.setCallback(new MenuPresenter.Callback() { + @Override + public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { + if (mDismissListener != null) { + mDismissListener.onDismiss(PopupMenu.this); + } + } + + @Override + public boolean onOpenSubMenu(MenuBuilder subMenu) { + // The menu presenter will handle opening the submenu itself. + // Nothing to do here. + return false; + } + }); } /** @@ -125,7 +138,6 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback { * the next time the popup is shown. * * @param gravity the gravity used to align the popup window - * * @see #getGravity() */ public void setGravity(int gravity) { @@ -134,7 +146,6 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback { /** * @return the gravity used to align the popup window to its anchor view - * * @see #setGravity(int) */ public int getGravity() { @@ -146,8 +157,8 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback { * to implement drag-to-open behavior. * <p> * When the listener is set on a view, touching that view and dragging - * outside of its bounds will open the popup window. Lifting will select the - * currently touched list item. + * outside of its bounds will open the popup window. Lifting will select + * the currently touched list item. * <p> * Example usage: * <pre> @@ -184,9 +195,10 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback { } /** - * @return the {@link Menu} associated with this popup. Populate the returned Menu with - * items before calling {@link #show()}. + * Returns the {@link Menu} associated with this popup. Populate the + * returned Menu with items before calling {@link #show()}. * + * @return the {@link Menu} associated with this popup * @see #show() * @see #getMenuInflater() */ @@ -195,9 +207,8 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback { } /** - * @return a {@link MenuInflater} that can be used to inflate menu items from XML into the - * menu returned by {@link #getMenu()}. - * + * @return a {@link MenuInflater} that can be used to inflate menu items + * from XML into the menu returned by {@link #getMenu()} * @see #getMenu() */ public MenuInflater getMenuInflater() { @@ -205,8 +216,9 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback { } /** - * Inflate a menu resource into this PopupMenu. This is equivalent to calling - * popupMenu.getMenuInflater().inflate(menuRes, popupMenu.getMenu()). + * Inflate a menu resource into this PopupMenu. This is equivalent to + * calling {@code popupMenu.getMenuInflater().inflate(menuRes, popupMenu.getMenu())}. + * * @param menuRes Menu resource to inflate */ public void inflate(@MenuRes int menuRes) { @@ -215,6 +227,7 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback { /** * Show the menu popup anchored to the view specified during construction. + * * @see #dismiss() */ public void show() { @@ -223,6 +236,7 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback { /** * Dismiss the menu popup. + * * @see #show() */ public void dismiss() { @@ -230,68 +244,49 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback { } /** - * Set a listener that will be notified when the user selects an item from the menu. + * Sets a listener that will be notified when the user selects an item from + * the menu. * - * @param listener Listener to notify + * @param listener the listener to notify */ public void setOnMenuItemClickListener(OnMenuItemClickListener listener) { mMenuItemClickListener = listener; } /** - * Set a listener that will be notified when this menu is dismissed. + * Sets a listener that will be notified when this menu is dismissed. * - * @param listener Listener to notify + * @param listener the listener to notify */ public void setOnDismissListener(OnDismissListener listener) { mDismissListener = listener; } /** - * @hide - */ - public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { - if (mMenuItemClickListener != null) { - return mMenuItemClickListener.onMenuItemClick(item); - } - return false; - } - - /** - * @hide + * Interface responsible for receiving menu item click events if the items + * themselves do not have individual item click listeners. */ - public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { - if (mDismissListener != null) { - mDismissListener.onDismiss(this); - } - } - - /** - * @hide - */ - public boolean onOpenSubMenu(MenuBuilder subMenu) { - // The menu presenter will handle opening the submenu itself. Nothing to do here. - return false; - } - - /** - * @hide - */ - public void onMenuModeChange(MenuBuilder menu) { + public interface OnMenuItemClickListener { + /** + * This method will be invoked when a menu item is clicked if the item + * itself did not already handle the event. + * + * @param item the menu item that was clicked + * @return {@code true} if the event was handled, {@code false} + * otherwise + */ + boolean onMenuItemClick(MenuItem item); } /** - * Interface responsible for receiving menu item click events if the items themselves - * do not have individual item click listeners. + * Callback interface used to notify the application that the menu has closed. */ - public interface OnMenuItemClickListener { + public interface OnDismissListener { /** - * This method will be invoked when a menu item is clicked if the item itself did - * not already handle the event. + * Called when the associated menu has been dismissed. * - * @param item {@link MenuItem} that was clicked - * @return <code>true</code> if the event was handled, <code>false</code> otherwise. + * @param menu the popup menu that was dismissed */ - public boolean onMenuItemClick(MenuItem item); + void onDismiss(PopupMenu menu); } } |