diff options
| author | Oren Blasberg <orenb@google.com> | 2015-09-08 14:59:12 -0700 |
|---|---|---|
| committer | Oren Blasberg <orenb@google.com> | 2015-09-16 13:47:03 -0700 |
| commit | 23087be7792c2d22173cf022a72d0648aa430ab5 (patch) | |
| tree | d3b26eb5615b3decdb2ef640dcd90ac9c2ced03e /core/java/android/widget/MenuPopupWindow.java | |
| parent | 9916282bac8ac8fab7ce5b649c049842acffa29b (diff) | |
Cascading popup menus: open submenu on mouse hover.
When the cascading feature is enabled, users can mouseover a
submenu item in a popup menu to expand and open the new submenu
(after a short timeout). Similarly, if a user mouseovers a
different menu item in the original menu, the submenu gets closed
(again, after a short timeout).
This should complete the implementation of cascading submenu
functionality.
Also fix two other issues:
(1) Update some oudated code in PopupMenu that was still opening
the submenu when a user clicks on a submenu item; this
responsibility now lives within the MenuPopupHelper's delegate
MenuPopup class, so it doesn't need to live in PopupMenu anymore.
(2) Fix an issue where icons would be force-set on a submenu when they
should not be. Instead, decide whether to show icons in a submenu
based on whether to show them in the top level menu, as intended.
Bug: 20127825
Change-Id: Ia46852c7f99436065ab4bc234de94dffc0019666
Diffstat (limited to 'core/java/android/widget/MenuPopupWindow.java')
| -rw-r--r-- | core/java/android/widget/MenuPopupWindow.java | 70 |
1 files changed, 65 insertions, 5 deletions
diff --git a/core/java/android/widget/MenuPopupWindow.java b/core/java/android/widget/MenuPopupWindow.java index 900aa326d502..ba77b1b53079 100644 --- a/core/java/android/widget/MenuPopupWindow.java +++ b/core/java/android/widget/MenuPopupWindow.java @@ -22,12 +22,12 @@ import android.content.res.Resources; import android.transition.Transition; import android.util.AttributeSet; import android.view.KeyEvent; +import android.view.MotionEvent; import android.view.View; -import android.view.ViewGroup; -import android.view.ViewParent; import com.android.internal.view.menu.ListMenuItemView; import com.android.internal.view.menu.MenuAdapter; +import com.android.internal.view.menu.MenuBuilder; /** * A MenuPopupWindow represents the popup window for menu. @@ -37,20 +37,32 @@ import com.android.internal.view.menu.MenuAdapter; * * @hide */ -public class MenuPopupWindow extends ListPopupWindow { +public class MenuPopupWindow extends ListPopupWindow implements MenuItemHoverListener { + private MenuItemHoverListener mHoverListener; + public MenuPopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override DropDownListView createDropDownListView(Context context, boolean hijackFocus) { - return new MenuDropDownListView(context, hijackFocus); + MenuDropDownListView view = new MenuDropDownListView(context, hijackFocus); + view.setHoverListener(this); + return view; } public void setEnterTransition(Transition enterTransition) { mPopup.setEnterTransition(enterTransition); } + public void setExitTransition(Transition exitTransition) { + mPopup.setExitTransition(exitTransition); + } + + public void setHoverListener(MenuItemHoverListener hoverListener) { + mHoverListener = hoverListener; + } + /** * Set whether this window is touch modal or if outside touches will be sent to * other windows behind it. @@ -59,10 +71,23 @@ public class MenuPopupWindow extends ListPopupWindow { mPopup.setTouchModal(touchModal); } - private static class MenuDropDownListView extends DropDownListView { + @Override + public void onItemHovered(MenuBuilder menu, int position) { + // Forward up the chain + if (mHoverListener != null) { + mHoverListener.onItemHovered(menu, position); + } + } + + /** + * @hide + */ + public static class MenuDropDownListView extends DropDownListView { final int mAdvanceKey; final int mRetreatKey; + private MenuItemHoverListener mHoverListener; + public MenuDropDownListView(Context context, boolean hijackFocus) { super(context, hijackFocus); @@ -77,6 +102,15 @@ public class MenuPopupWindow extends ListPopupWindow { } } + public void setHoverListener(MenuItemHoverListener hoverListener) { + mHoverListener = hoverListener; + } + + public void clearSelection() { + setSelectedPositionInt(INVALID_POSITION); + setNextSelectedPositionInt(INVALID_POSITION); + } + @Override public boolean onKeyDown(int keyCode, KeyEvent event) { ListMenuItemView selectedItem = (ListMenuItemView) getSelectedView(); @@ -99,6 +133,32 @@ public class MenuPopupWindow extends ListPopupWindow { return super.onKeyDown(keyCode, event); } + @Override + public boolean onHoverEvent(MotionEvent ev) { + boolean dispatchHover = false; + final int position = pointToPosition((int) ev.getX(), (int) ev.getY()); + + final int action = ev.getActionMasked(); + if (action == MotionEvent.ACTION_HOVER_ENTER + || action == MotionEvent.ACTION_HOVER_MOVE) { + if (position != INVALID_POSITION && position != mSelectedPosition) { + final View hoveredItem = getChildAt(position - getFirstVisiblePosition()); + if (hoveredItem.isEnabled()) { + dispatchHover = true; + } + } + } + + boolean superVal = super.onHoverEvent(ev); + + if (dispatchHover && mHoverListener != null) { + mHoverListener.onItemHovered( + ((MenuAdapter) getAdapter()).getAdapterMenu(), position); + } + + return superVal; + } } + }
\ No newline at end of file |
