Class XpListPopupWindow

  • All Implemented Interfaces:
    androidx.appcompat.view.menu.ShowableListMenu

    public class XpListPopupWindow
    extends java.lang.Object
    See Also:
    ListPopupWindow, ListPopupWindow
    • Field Detail

      • WIDTH_MATCH_CONSTRAINT

        public static final int WIDTH_MATCH_CONSTRAINT
        Popup menu width is only limited by maxWidth.
        See Also:
        Constant Field Values
      • WIDTH_WRAP_CONTENT

        public static final int WIDTH_WRAP_CONTENT
        Popup menu width is * at least as wide as its content, * limited by maxWidth.
        See Also:
        Constant Field Values
      • WIDTH_WRAP_CONTENT_UNIT

        public static final int WIDTH_WRAP_CONTENT_UNIT
        Popup menu width is
        • at least as wide as its content rounded up to a multiple of widthUnit,
        • at least as wide as widthUnit * 1.5,
        • limited by maxWidth.
        See Also:
        Constant Field Values
      • INPUT_METHOD_FROM_FOCUSABLE

        public static final int INPUT_METHOD_FROM_FOCUSABLE
        Mode for 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.
        See Also:
        Constant Field Values
      • INPUT_METHOD_NEEDED

        public static final int INPUT_METHOD_NEEDED
        Mode for 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.
        See Also:
        Constant Field Values
      • INPUT_METHOD_NOT_NEEDED

        public static final 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. 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.
        See Also:
        Constant Field Values
    • Constructor Detail

      • XpListPopupWindow

        public XpListPopupWindow​(@NonNull
                                 android.content.Context context)
      • XpListPopupWindow

        public XpListPopupWindow​(@NonNull
                                 android.content.Context context,
                                 @Nullable
                                 android.util.AttributeSet attrs)
      • XpListPopupWindow

        public XpListPopupWindow​(@NonNull
                                 android.content.Context context,
                                 @Nullable
                                 android.util.AttributeSet attrs,
                                 @AttrRes
                                 int defStyleAttr)
      • XpListPopupWindow

        public XpListPopupWindow​(@NonNull
                                 android.content.Context context,
                                 @Nullable
                                 android.util.AttributeSet attrs,
                                 @AttrRes
                                 int defStyleAttr,
                                 @StyleRes
                                 int defStyleRes)
    • Method Detail

      • setMarginTop

        public void setMarginTop​(int px)
      • getMarginTop

        public int getMarginTop()
      • setMarginBottom

        public void setMarginBottom​(int px)
      • getMarginBottom

        public int getMarginBottom()
      • setMarginLeft

        public void setMarginLeft​(int px)
      • setMarginStart

        public void setMarginStart​(int px)
      • getMarginStart

        @Deprecated
        public int getMarginStart​(int px)
        Deprecated.
      • getMarginStart

        public int getMarginStart()
      • getMarginLeft

        public int getMarginLeft()
      • setMarginRight

        public void setMarginRight​(int px)
      • setMarginEnd

        public void setMarginEnd​(int px)
      • getMarginEnd

        @Deprecated
        public int getMarginEnd​(int px)
        Deprecated.
      • getMarginEnd

        public int getMarginEnd()
      • getMarginRight

        public int getMarginRight()
      • setMargin

        public void setMargin​(int margin)
      • setMargin

        public void setMargin​(int horizontal,
                              int vertical)
      • setMargin

        public void setMargin​(int left,
                              int top,
                              int right,
                              int bottom)
      • setMarginRelative

        public void setMarginRelative​(int start,
                                      int top,
                                      int end,
                                      int bottom)
      • hasMultilineItems

        public boolean hasMultilineItems()
        Returns:
        Whether the list in current setup would show any multiline items.
      • hasMultiLineItems

        @Deprecated
        public boolean hasMultiLineItems()
        Deprecated.
        Returns:
        Whether the list in current setup would show any multiline items.
      • setAdapter

        public void setAdapter​(@Nullable
                               android.widget.ListAdapter adapter)
        Sets the adapter that provides the data and the views to represent the data in this popup window.
        Parameters:
        adapter - The adapter to use to create this window's content.
      • setModal

        public void setModal​(boolean modal)
        Set whether this window should be modal when shown.

        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.

        Parameters:
        modal - true if the popup window should be modal, false otherwise.
      • isModal

        public boolean isModal()
        Returns whether the popup window will be modal when shown.
        Returns:
        true if the popup window will be modal, false otherwise.
      • setForceIgnoreOutsideTouch

        public void setForceIgnoreOutsideTouch​(boolean forceIgnoreOutsideTouch)
        Forces outside touches to be ignored. Normally if 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.
      • setDropDownAlwaysVisible

        public void setDropDownAlwaysVisible​(boolean dropDownAlwaysVisible)
        Sets whether the drop-down should remain visible under certain conditions.

        The drop-down will occupy the entire screen below getAnchorView() regardless of the size or content of the list. getBackground() will fill any space that is not used by the list.

        Parameters:
        dropDownAlwaysVisible - Whether to keep the drop-down visible.
      • isDropDownAlwaysVisible

        public boolean isDropDownAlwaysVisible()
        Returns:
        Whether the drop-down is visible under special conditions.
      • setSoftInputMode

        public void setSoftInputMode​(int mode)
        Sets the operating mode for the soft input area.
        Parameters:
        mode - The desired mode, see WindowManager.LayoutParams.softInputMode for the full list
        See Also:
        WindowManager.LayoutParams.softInputMode, getSoftInputMode()
      • setListSelector

        public void setListSelector​(@Nullable
                                    android.graphics.drawable.Drawable selector)
        Sets a drawable to use as the list item selector.
        Parameters:
        selector - List selector drawable to use in the popup.
      • getBackground

        @Nullable
        public android.graphics.drawable.Drawable getBackground()
        Returns:
        The background drawable for the popup window.
      • setBackgroundDrawable

        public void setBackgroundDrawable​(@Nullable
                                          android.graphics.drawable.Drawable d)
        Sets a drawable to be the background for the popup window.
        Parameters:
        d - A drawable to set as the background.
      • setAnimationStyle

        public void setAnimationStyle​(int animationStyle)
        Set an animation style to use when the popup window is shown or dismissed.
        Parameters:
        animationStyle - Animation style to use.
      • getAnimationStyle

        @StyleRes
        public int getAnimationStyle()
        Returns the animation style that will be used when the popup window is shown or dismissed.
        Returns:
        Animation style that will be used.
      • getAnchorView

        @Nullable
        public android.view.View getAnchorView()
        Returns the view that will be used to anchor this popup.
        Returns:
        The popup's anchor view
      • setAnchorView

        public void setAnchorView​(@Nullable
                                  android.view.View anchor)
        Sets the popup's anchor view. This popup will always be positioned relative to the anchor view when shown.
        Parameters:
        anchor - The view to use as an anchor.
      • getBoundsView

        @Nullable
        public android.view.View getBoundsView()
      • setBoundsView

        public void setBoundsView​(@Nullable
                                  android.view.View bounds)
      • getHorizontalOffset

        public int getHorizontalOffset()
        Returns:
        The horizontal offset of the popup from its anchor in pixels.
      • setHorizontalOffset

        public void setHorizontalOffset​(int offset)
        Set the horizontal offset of this popup from its anchor view in pixels.
        Parameters:
        offset - The horizontal offset of the popup from its anchor.
      • getVerticalOffset

        public int getVerticalOffset()
        Returns:
        The vertical offset of the popup from its anchor in pixels.
      • setVerticalOffset

        public void setVerticalOffset​(int offset)
        Set the vertical offset of this popup from its anchor view in pixels.
        Parameters:
        offset - The vertical offset of the popup from its anchor.
      • setEpicenterBounds

        public void setEpicenterBounds​(@Nullable
                                       android.graphics.Rect bounds)
        Specifies the anchor-relative bounds of the popup's transition epicenter.
        Parameters:
        bounds - anchor-relative bounds
      • setDropDownGravity

        public void setDropDownGravity​(int gravity)
        Set the gravity of the dropdown list. This is commonly used to set gravity to START or END for alignment with the anchor.
        Parameters:
        gravity - Gravity value to use
      • getDropDownGravity

        public int getDropDownGravity()
      • getWidth

        public int getWidth()
        Returns:
        The width of the popup window in pixels.
      • getMaxWidth

        public int getMaxWidth()
      • getPreferredWidthUnit

        @Deprecated
        @Dimension
        public float getPreferredWidthUnit()
        Deprecated.
      • getWidthUnit

        @Dimension
        public float getWidthUnit()
        Returns:
        Min width unit size.
      • setMaxWidth

        public void setMaxWidth​(int maxWidth)
        Sets the maximum width of the popup menu in pixels. Can also be MAX_WIDTH_FIT_SCREEN or MAX_WIDTH_FIT_ANCHOR.
        Parameters:
        maxWidth - Maximum width of the popup window.
      • setPreferredWidthUnit

        @Deprecated
        public void setPreferredWidthUnit​(@Dimension
                                          float unit)
        Deprecated.
        See Also:
        setWidthUnit(float)
      • setContentWidth

        public void setContentWidth​(int width)
        Sets the width of the popup window by the size of its content. The final width may be larger to accommodate styled window dressing.
        Parameters:
        width - Desired width of content in pixels.
      • getHeight

        public int getHeight()
        Returns:
        The height of the popup window in pixels.
      • setHeight

        public void setHeight​(int height)
        Sets the height of the popup window in pixels. Can also be MATCH_PARENT.
        Parameters:
        height - Height of the popup window.
        Throws:
        java.lang.IllegalArgumentException - if height is set to negative value
      • setMaxItemCount

        public void setMaxItemCount​(int maxItemCount)
        Parameters:
        maxItemCount - Popup menu will adjust its height to display at most this many items.
      • setWindowLayoutType

        public void setWindowLayoutType​(int layoutType)
        Set the layout type for this popup window.

        See WindowManager.LayoutParams.type for possible values.

        Parameters:
        layoutType - Layout type for this window.
        See Also:
        WindowManager.LayoutParams.type
      • setOnItemClickListener

        public void setOnItemClickListener​(@Nullable
                                           android.widget.AdapterView.OnItemClickListener clickListener)
        Sets a listener to receive events when a list item is clicked.
        Parameters:
        clickListener - Listener to register
        See Also:
        AdapterView.setOnItemClickListener(AdapterView.OnItemClickListener)
      • setOnItemSelectedListener

        public void setOnItemSelectedListener​(@Nullable
                                              android.widget.AdapterView.OnItemSelectedListener selectedListener)
        Sets a listener to receive events when a list item is selected.
        Parameters:
        selectedListener - Listener to register.
        See Also:
        AdapterView.setOnItemSelectedListener(AdapterView.OnItemSelectedListener)
      • setPromptView

        public void setPromptView​(@Nullable
                                  android.view.View prompt)
        Set a view to act as a user prompt for this popup window. Where the prompt view will appear is controlled by setPromptPosition(int).
        Parameters:
        prompt - View to use as an informational prompt.
      • postShow

        public void postShow()
        Post a ShowableListMenu.show() call to the UI thread.
      • show

        @MainThread
        public void show()
        Show the popup list. If the list is already showing, this method will do nothing.
        Specified by:
        show in interface androidx.appcompat.view.menu.ShowableListMenu
      • dismiss

        public void dismiss()
        Dismiss the popup window.
        Specified by:
        dismiss in interface androidx.appcompat.view.menu.ShowableListMenu
      • setOnDismissListener

        public void setOnDismissListener​(@Nullable
                                         android.widget.PopupWindow.OnDismissListener listener)
        Set a listener to receive a callback when the popup is dismissed.
        Parameters:
        listener - Listener that will be notified when the popup is dismissed.
      • setSelection

        public void setSelection​(int position)
        Mark item on specified position checked, selected and positioned over anchor view.
        Parameters:
        position - Selected item index.
      • measurePreferredVerticalOffset

        public void measurePreferredVerticalOffset​(int position)
        Compute preferred vertical offset of popup window top from anchor bottom so that selected item in the popup window is drawn precisely over the anchor. It also calculates selected item view height.
        Parameters:
        position - Which item is supposed to be selected, and preferably aligned over anchor.
        See Also:
        getMeasuredPreferredVerticalOffset(), getMeasuredSelectedItemViewHeight()
      • getPreferredVerticalOffset

        @Deprecated
        public int getPreferredVerticalOffset​(int position)
        Deprecated.
        This method pre-calculates multiple values. Use specialized accessor methods.
        Measures popup offset and selected item scroll offset for selected item to be positioned exactly over anchor.
        Parameters:
        position - Which item is supposed to be selected, and preferably aligned over anchor.
        Returns:
        Measured vertical offset for popup window.
        See Also:
        measurePreferredVerticalOffset(int), getMeasuredPreferredVerticalOffset(), getMeasuredSelectedItemViewHeight()
      • clearListSelection

        public void clearListSelection()
        Clear any current list selection. Only valid when ShowableListMenu.isShowing() == true.
      • isShowing

        public boolean isShowing()
        Specified by:
        isShowing in interface androidx.appcompat.view.menu.ShowableListMenu
        Returns:
        true if the popup is currently showing, false otherwise.
      • isInputMethodNotNeeded

        public boolean isInputMethodNotNeeded()
        Returns:
        true if this popup is configured to assume the user does not need to interact with the IME while it is showing, false otherwise.
      • performItemClick

        public boolean performItemClick​(int position)
        Perform an item click operation on the specified list adapter position.
        Parameters:
        position - Adapter position for performing the click
        Returns:
        true if the click action could be performed, false if not. (e.g. if the popup was not showing, this method would return false.)
      • getSelectedItem

        @Nullable
        public java.lang.Object getSelectedItem()
        Returns:
        The currently selected item or null if the popup is not showing.
      • getSelectedItemPosition

        public int getSelectedItemPosition()
        Returns:
        The position of the currently selected item or AdapterView.INVALID_POSITION if ShowableListMenu.isShowing() == false.
        See Also:
        AdapterView.getSelectedItemPosition()
      • getSelectedItemId

        public long getSelectedItemId()
        Returns:
        The ID of the currently selected item or AdapterView.INVALID_ROW_ID if ShowableListMenu.isShowing() == false.
        See Also:
        AdapterView.getSelectedItemId()
      • getSelectedView

        @Nullable
        public android.view.View getSelectedView()
        Returns:
        The View for the currently selected item or null if ShowableListMenu.isShowing() == false.
        See Also:
        AbsListView.getSelectedView()
      • getListView

        @Nullable
        public android.widget.ListView getListView()
        Specified by:
        getListView in interface androidx.appcompat.view.menu.ShowableListMenu
        Returns:
        The ListView displayed within the popup window. Only valid when ShowableListMenu.isShowing() == true.
      • createDragToOpenListener

        @Deprecated
        @NonNull
        public android.view.View.OnTouchListener createDragToOpenListener​(@NonNull
                                                                          android.view.View src)
        Deprecated.
        This feature is currently not supported.
        Returns an 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);
         
        Parameters:
        src - the view on which the resulting listener will be set
        Returns:
        a touch listener that controls drag-to-open behavior