diff --git a/api/current.txt b/api/current.txt index 4b66d086b2cb1..11c2ccc97f3f2 100644 --- a/api/current.txt +++ b/api/current.txt @@ -31382,6 +31382,7 @@ package android.widget { ctor public ListPopupWindow(android.content.Context, android.util.AttributeSet, int); ctor public ListPopupWindow(android.content.Context, android.util.AttributeSet, int, int); method public void clearListSelection(); + method public android.view.View.OnTouchListener createDragToOpenListener(android.view.View); method public void dismiss(); method public android.view.View getAnchorView(); method public int getAnimationStyle(); @@ -31593,6 +31594,7 @@ package android.widget { public class PopupMenu { ctor public PopupMenu(android.content.Context, android.view.View); method public void dismiss(); + method public android.view.View.OnTouchListener getDragToOpenListener(); method public android.view.Menu getMenu(); method public android.view.MenuInflater getMenuInflater(); method public void inflate(int); diff --git a/core/java/android/widget/ListPopupWindow.java b/core/java/android/widget/ListPopupWindow.java index 6262387462239..f2da765745fc7 100644 --- a/core/java/android/widget/ListPopupWindow.java +++ b/core/java/android/widget/ListPopupWindow.java @@ -964,6 +964,33 @@ public class ListPopupWindow { return false; } + /** + * Returns an {@link OnTouchListener} that can be added to the source view + * to implement drag-to-open behavior. Generally, the source view should be + * the same view that was passed to {@link #setAnchorView}. + *

+ * 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. + *

+ * Example usage: + *

ListPopupWindow myPopup = new ListPopupWindow(context);
+     * myPopup.setAnchor(myAnchor);
+     * OnTouchListener dragListener = myPopup.createDragToOpenListener(myAnchor);
+     * myAnchor.setOnTouchListener(dragListener);
+ * + * @param src the view on which the resulting listener will be set + * @return a touch listener that controls drag-to-open behavior + */ + public OnTouchListener createDragToOpenListener(View src) { + return new ForwardingListener(src) { + @Override + public ListPopupWindow getPopup() { + return ListPopupWindow.this; + } + }; + } + /** *

Builds the popup window's content and returns the height the popup * should have. Returns -1 when the content already exists.

diff --git a/core/java/android/widget/PopupMenu.java b/core/java/android/widget/PopupMenu.java index 6a6d767661138..e5344c6afbcf1 100644 --- a/core/java/android/widget/PopupMenu.java +++ b/core/java/android/widget/PopupMenu.java @@ -26,6 +26,8 @@ import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; +import android.view.View.OnTouchListener; +import android.widget.ListPopupWindow.ForwardingListener; /** * A PopupMenu displays a {@link Menu} in a modal popup window anchored to a {@link View}. @@ -40,6 +42,7 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback { private MenuPopupHelper mPopup; private OnMenuItemClickListener mMenuItemClickListener; private OnDismissListener mDismissListener; + private OnTouchListener mDragListener; /** * Callback interface used to notify the application that the menu has closed. @@ -70,6 +73,33 @@ public class PopupMenu implements MenuBuilder.Callback, MenuPresenter.Callback { mPopup.setCallback(this); } + /** + * Returns an {@link OnTouchListener} that can be added to the anchor view + * to implement drag-to-open behavior. + *

+ * 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. + *

+ * Example usage: + *

PopupMenu myPopup = new PopupMenu(context, myAnchor);
+     * myAnchor.setOnTouchListener(myPopup.getDragToOpenListener());
+ * + * @return a touch listener that controls drag-to-open behavior + */ + public OnTouchListener getDragToOpenListener() { + if (mDragListener == null) { + mDragListener = new ForwardingListener(mAnchor) { + @Override + public ListPopupWindow getPopup() { + return mPopup.getPopup(); + } + }; + } + + return mDragListener; + } + /** * @return the {@link Menu} associated with this popup. Populate the returned Menu with * items before calling {@link #show()}.