public class ListPopupWindow extends Object implements ShowableListMenu
ListPopupWindow
.
Used to write apps that run on platforms prior to Android L. When running
on Android L or above, this implementation is still used; it does not try
to switch to the framework's implementation. See the framework SDK
documentation for a class overview.ListPopupWindow
Modifier and Type | Field and Description |
---|---|
static int |
INPUT_METHOD_FROM_FOCUSABLE
Mode for
setInputMethodMode(int) : the requirements for the
input method should be based on the focusability of the popup. |
static int |
INPUT_METHOD_NEEDED
Mode for
setInputMethodMode(int) : this popup always needs to
work with an input method, regardless of whether it is focusable. |
static int |
INPUT_METHOD_NOT_NEEDED
Mode for
setInputMethodMode(int) : this popup never needs to
work with an input method, regardless of whether it is focusable. |
static int |
MATCH_PARENT
Alias for
ViewGroup.LayoutParams#MATCH_PARENT . |
static int |
POSITION_PROMPT_ABOVE
The provided prompt view should appear above list content.
|
static int |
POSITION_PROMPT_BELOW
The provided prompt view should appear below list content.
|
static int |
WRAP_CONTENT
Alias for
ViewGroup.LayoutParams#WRAP_CONTENT . |
Constructor and Description |
---|
ListPopupWindow(Context context)
Create a new, empty popup window capable of displaying items from a ListAdapter.
|
ListPopupWindow(Context context,
AttributeSet attrs)
Create a new, empty popup window capable of displaying items from a ListAdapter.
|
ListPopupWindow(Context context,
AttributeSet attrs,
int defStyleAttr)
Create a new, empty popup window capable of displaying items from a ListAdapter.
|
ListPopupWindow(Context context,
AttributeSet attrs,
int defStyleAttr,
int defStyleRes)
Create a new, empty popup window capable of displaying items from a ListAdapter.
|
Modifier and Type | Method and Description |
---|---|
void |
clearListSelection()
Clear any current list selection.
|
View.OnTouchListener |
createDragToOpenListener(View src)
Returns an
View.OnTouchListener that can be added to the source view
to implement drag-to-open behavior. |
void |
dismiss()
Dismiss the popup window.
|
View |
getAnchorView()
Returns the view that will be used to anchor this popup.
|
int |
getAnimationStyle()
Returns the animation style that will be used when the popup window is
shown or dismissed.
|
Drawable |
getBackground() |
int |
getHeight() |
int |
getHorizontalOffset() |
int |
getInputMethodMode()
Return the current value in
setInputMethodMode(int) . |
ListView |
getListView() |
int |
getPromptPosition() |
Object |
getSelectedItem() |
long |
getSelectedItemId() |
int |
getSelectedItemPosition() |
View |
getSelectedView() |
int |
getSoftInputMode()
Returns the current value in
setSoftInputMode(int) . |
int |
getVerticalOffset() |
int |
getWidth() |
boolean |
isDropDownAlwaysVisible() |
boolean |
isInputMethodNotNeeded() |
boolean |
isModal()
Returns whether the popup window will be modal when shown.
|
boolean |
isShowing() |
boolean |
onKeyDown(int keyCode,
KeyEvent event)
Filter key down events.
|
boolean |
onKeyPreIme(int keyCode,
KeyEvent event)
Filter pre-IME key events.
|
boolean |
onKeyUp(int keyCode,
KeyEvent event)
Filter key down events.
|
boolean |
performItemClick(int position)
Perform an item click operation on the specified list adapter position.
|
void |
postShow()
Post a
show() call to the UI thread. |
void |
setAdapter(ListAdapter adapter)
Sets the adapter that provides the data and the views to represent the data
in this popup window.
|
void |
setAnchorView(View anchor)
Sets the popup's anchor view.
|
void |
setAnimationStyle(int animationStyle)
Set an animation style to use when the popup window is shown or dismissed.
|
void |
setBackgroundDrawable(Drawable d)
Sets a drawable to be the background for the popup window.
|
void |
setContentWidth(int width)
Sets the width of the popup window by the size of its content.
|
void |
setDropDownAlwaysVisible(boolean dropDownAlwaysVisible)
Sets whether the drop-down should remain visible under certain conditions.
|
void |
setDropDownGravity(int gravity)
Set the gravity of the dropdown list.
|
void |
setEpicenterBounds(Rect bounds)
Specifies the anchor-relative bounds of the popup's transition
epicenter.
|
void |
setForceIgnoreOutsideTouch(boolean forceIgnoreOutsideTouch)
Forces outside touches to be ignored.
|
void |
setHeight(int height)
Sets the height of the popup window in pixels.
|
void |
setHorizontalOffset(int offset)
Set the horizontal offset of this popup from its anchor view in pixels.
|
void |
setInputMethodMode(int mode)
Control how the popup operates with an input method: one of
INPUT_METHOD_FROM_FOCUSABLE , INPUT_METHOD_NEEDED ,
or INPUT_METHOD_NOT_NEEDED . |
void |
setListSelector(Drawable selector)
Sets a drawable to use as the list item selector.
|
void |
setModal(boolean modal)
Set whether this window should be modal when shown.
|
void |
setOnDismissListener(PopupWindow.OnDismissListener listener)
Set a listener to receive a callback when the popup is dismissed.
|
void |
setOnItemClickListener(AdapterView.OnItemClickListener clickListener)
Sets a listener to receive events when a list item is clicked.
|
void |
setOnItemSelectedListener(AdapterView.OnItemSelectedListener selectedListener)
Sets a listener to receive events when a list item is selected.
|
void |
setPromptPosition(int position)
Set where the optional prompt view should appear.
|
void |
setPromptView(View prompt)
Set a view to act as a user prompt for this popup window.
|
void |
setSelection(int position)
Set the selected position of the list.
|
void |
setSoftInputMode(int mode)
Sets the operating mode for the soft input area.
|
void |
setVerticalOffset(int offset)
Set the vertical offset of this popup from its anchor view in pixels.
|
void |
setWidth(int width)
Sets the width of the popup window in pixels.
|
void |
setWindowLayoutType(int layoutType)
Set the layout type for this popup window.
|
void |
show()
Show the popup list.
|
public static final int POSITION_PROMPT_ABOVE
public static final int POSITION_PROMPT_BELOW
public static final int MATCH_PARENT
ViewGroup.LayoutParams#MATCH_PARENT
.
If used to specify a popup width, the popup will match the width of the anchor view.
If used to specify a popup height, the popup will fill available space.public static final int WRAP_CONTENT
ViewGroup.LayoutParams#WRAP_CONTENT
.
If used to specify a popup width, the popup will use the width of its content.public static final int INPUT_METHOD_FROM_FOCUSABLE
setInputMethodMode(int)
: the requirements for the
input method should be based on the focusability of the popup. That is
if it is focusable than it needs to work with the input method, else
it doesn't.public static final int INPUT_METHOD_NEEDED
setInputMethodMode(int)
: this popup always needs to
work with an input method, regardless of whether it is focusable. This
means that it will always be displayed so that the user can also operate
the input method while it is shown.public static final int INPUT_METHOD_NOT_NEEDED
setInputMethodMode(int)
: this popup never needs to
work with an input method, regardless of whether it is focusable. This
means that it will always be displayed to use as much space on the
screen as needed, regardless of whether this covers the input method.public ListPopupWindow(@NonNull Context context)
setBackgroundDrawable(Drawable)
.context
- Context used for contained views.public ListPopupWindow(@NonNull Context context, @Nullable AttributeSet attrs)
setBackgroundDrawable(Drawable)
.context
- Context used for contained views.attrs
- Attributes from inflating parent views used to style the popup.public ListPopupWindow(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr)
setBackgroundDrawable(Drawable)
.context
- Context used for contained views.attrs
- Attributes from inflating parent views used to style the popup.defStyleAttr
- Default style attribute to use for popup content.public ListPopupWindow(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes)
setBackgroundDrawable(Drawable)
.context
- Context used for contained views.attrs
- Attributes from inflating parent views used to style the popup.defStyleAttr
- Style attribute to read for default styling of popup content.defStyleRes
- Style resource ID to use for default styling of popup content.public void setAdapter(@Nullable ListAdapter adapter)
adapter
- The adapter to use to create this window's content.public void setPromptPosition(int position)
POSITION_PROMPT_ABOVE
.position
- A position constant declaring where the prompt should be displayed.POSITION_PROMPT_ABOVE
,
POSITION_PROMPT_BELOW
public int getPromptPosition()
POSITION_PROMPT_ABOVE
,
POSITION_PROMPT_BELOW
public void setModal(boolean modal)
If a popup window is modal, it will receive all touch and key input. If the user touches outside the popup window's content area the popup window will be dismissed.
modal
- true
if the popup window should be modal, false
otherwise.public boolean isModal()
true
if the popup window will be modal, false
otherwise.public void setForceIgnoreOutsideTouch(boolean forceIgnoreOutsideTouch)
isDropDownAlwaysVisible()
is
false, we allow outside touch to dismiss the dropdown. If this is set to true, then we
ignore outside touch even when the drop down is not set to always visible.public void setDropDownAlwaysVisible(boolean dropDownAlwaysVisible)
getAnchorView()
regardless
of the size or content of the list. getBackground()
will fill any space
that is not used by the list.dropDownAlwaysVisible
- Whether to keep the drop-down visible.public boolean isDropDownAlwaysVisible()
public void setSoftInputMode(int mode)
mode
- The desired mode, see
WindowManager.LayoutParams.softInputMode
for the full listWindowManager.LayoutParams.softInputMode
,
getSoftInputMode()
public int getSoftInputMode()
setSoftInputMode(int)
.public void setListSelector(Drawable selector)
selector
- List selector drawable to use in the popup.@Nullable public Drawable getBackground()
public void setBackgroundDrawable(@Nullable Drawable d)
d
- A drawable to set as the background.public void setAnimationStyle(@StyleRes int animationStyle)
animationStyle
- Animation style to use.@StyleRes public int getAnimationStyle()
@Nullable public View getAnchorView()
public void setAnchorView(@Nullable View anchor)
anchor
- The view to use as an anchor.public int getHorizontalOffset()
public void setHorizontalOffset(int offset)
offset
- The horizontal offset of the popup from its anchor.public int getVerticalOffset()
public void setVerticalOffset(int offset)
offset
- The vertical offset of the popup from its anchor.public void setEpicenterBounds(Rect bounds)
bounds
- anchor-relative boundspublic void setDropDownGravity(int gravity)
gravity
- Gravity value to usepublic int getWidth()
public void setWidth(int width)
MATCH_PARENT
or WRAP_CONTENT
.width
- Width of the popup window.public void setContentWidth(int width)
width
- Desired width of content in pixels.public int getHeight()
public void setHeight(int height)
MATCH_PARENT
.height
- Height of the popup window.public void setWindowLayoutType(int layoutType)
See WindowManager.LayoutParams#type
for possible values.
layoutType
- Layout type for this window.WindowManager.LayoutParams#type
public void setOnItemClickListener(@Nullable AdapterView.OnItemClickListener clickListener)
clickListener
- Listener to registerAdapterView.setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)
public void setOnItemSelectedListener(@Nullable AdapterView.OnItemSelectedListener selectedListener)
selectedListener
- Listener to register.AdapterView.setOnItemSelectedListener(OnItemSelectedListener)
public void setPromptView(@Nullable View prompt)
setPromptPosition(int)
.prompt
- View to use as an informational prompt.public void postShow()
show()
call to the UI thread.public void show()
show
in interface ShowableListMenu
public void dismiss()
dismiss
in interface ShowableListMenu
public void setOnDismissListener(@Nullable PopupWindow.OnDismissListener listener)
listener
- Listener that will be notified when the popup is dismissed.public void setInputMethodMode(int mode)
INPUT_METHOD_FROM_FOCUSABLE
, INPUT_METHOD_NEEDED
,
or INPUT_METHOD_NOT_NEEDED
.
If the popup is showing, calling this method will take effect only
the next time the popup is shown or through a manual call to the show()
method.
getInputMethodMode()
,
show()
public int getInputMethodMode()
setInputMethodMode(int)
.setInputMethodMode(int)
public void setSelection(int position)
isShowing()
== true
.position
- List position to set as selected.public void clearListSelection()
isShowing()
== true
.public boolean isShowing()
isShowing
in interface ShowableListMenu
true
if the popup is currently showing, false
otherwise.public boolean isInputMethodNotNeeded()
true
if this popup is configured to assume the user does not need
to interact with the IME while it is showing, false
otherwise.public boolean performItemClick(int position)
position
- Adapter position for performing the click@Nullable public Object getSelectedItem()
public int getSelectedItemPosition()
AdapterView.INVALID_POSITION
if isShowing()
== false
.AdapterView.getSelectedItemPosition()
public long getSelectedItemId()
AdapterView.INVALID_ROW_ID
if isShowing()
== false
.AdapterView.getSelectedItemId()
@Nullable public View getSelectedView()
isShowing()
== false
.AbsListView.getSelectedView()
@Nullable public ListView getListView()
getListView
in interface ShowableListMenu
ListView
displayed within the popup window.
Only valid when isShowing()
== true
.public boolean onKeyDown(int keyCode, @NonNull KeyEvent event)
keyCode
- keyCode param passed to the host view's onKeyDownevent
- event param passed to the host view's onKeyDownsetModal(boolean)
public boolean onKeyUp(int keyCode, @NonNull KeyEvent event)
keyCode
- keyCode param passed to the host view's onKeyUpevent
- event param passed to the host view's onKeyUpsetModal(boolean)
public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event)
View.onKeyPreIme(int, KeyEvent)
events to this function, views using ListPopupWindow can have it dismiss the popup
when the back key is pressed.keyCode
- keyCode param passed to the host view's onKeyPreImeevent
- event param passed to the host view's onKeyPreImesetModal(boolean)
public View.OnTouchListener createDragToOpenListener(View src)
View.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 setAnchorView(android.view.View)
.
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);
src
- the view on which the resulting listener will be set