Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package androidx.core.widget;
     18 
     19 import android.os.Build;
     20 import android.view.View;
     21 import android.view.View.OnTouchListener;
     22 import android.widget.ListPopupWindow;
     23 
     24 import androidx.annotation.NonNull;
     25 import androidx.annotation.Nullable;
     26 
     27 /**
     28  * Helper for accessing features in {@link ListPopupWindow}.
     29  */
     30 public final class ListPopupWindowCompat {
     31     private ListPopupWindowCompat() {
     32         // This class is not publicly instantiable.
     33     }
     34 
     35     /**
     36      * On API {@link android.os.Build.VERSION_CODES#KITKAT} and higher, returns
     37      * an {@link OnTouchListener} that can be added to the source view to
     38      * implement drag-to-open behavior. Generally, the source view should be the
     39      * same view that was passed to ListPopupWindow.setAnchorView(View).
     40      * <p>
     41      * When the listener is set on a view, touching that view and dragging
     42      * outside of its bounds will open the popup window. Lifting will select the
     43      * currently touched list item.
     44      * <p>
     45      * Example usage:
     46      *
     47      * <pre>
     48      * ListPopupWindow myPopup = new ListPopupWindow(context);
     49      * myPopup.setAnchor(myAnchor);
     50      * OnTouchListener dragListener = myPopup.createDragToOpenListener(myAnchor);
     51      * myAnchor.setOnTouchListener(dragListener);
     52      * </pre>
     53      *
     54      * @param listPopupWindow the ListPopupWindow against which to invoke the
     55      *            method
     56      * @param src the view on which the resulting listener will be set
     57      * @return a touch listener that controls drag-to-open behavior, or {@code null} on
     58      *         unsupported APIs
     59      *
     60      * @deprecated Use {@link #createDragToOpenListener(ListPopupWindow, View)} that takes in
     61      * {@link ListPopupWindow} instead of {@link Object}.
     62      */
     63     @Deprecated
     64     public static OnTouchListener createDragToOpenListener(Object listPopupWindow, View src) {
     65         return ListPopupWindowCompat.createDragToOpenListener(
     66                 (ListPopupWindow) listPopupWindow, src);
     67     }
     68 
     69     /**
     70      * On API {@link android.os.Build.VERSION_CODES#KITKAT} and higher, returns
     71      * an {@link OnTouchListener} that can be added to the source view to
     72      * implement drag-to-open behavior. Generally, the source view should be the
     73      * same view that was passed to ListPopupWindow.setAnchorView(View).
     74      * <p>
     75      * When the listener is set on a view, touching that view and dragging
     76      * outside of its bounds will open the popup window. Lifting will select the
     77      * currently touched list item.
     78      * <p>
     79      * Example usage:
     80      *
     81      * <pre>
     82      * ListPopupWindow myPopup = new ListPopupWindow(context);
     83      * myPopup.setAnchor(myAnchor);
     84      * OnTouchListener dragListener = myPopup.createDragToOpenListener(myAnchor);
     85      * myAnchor.setOnTouchListener(dragListener);
     86      * </pre>
     87      *
     88      * @param listPopupWindow the ListPopupWindow against which to invoke the
     89      *            method
     90      * @param src the view on which the resulting listener will be set
     91      * @return a touch listener that controls drag-to-open behavior, or {@code null} on
     92      *         unsupported APIs
     93      */
     94     @Nullable
     95     public static OnTouchListener createDragToOpenListener(
     96             @NonNull ListPopupWindow listPopupWindow, @NonNull View src) {
     97         if (Build.VERSION.SDK_INT >= 19) {
     98             return listPopupWindow.createDragToOpenListener(src);
     99         } else {
    100             return null;
    101         }
    102     }
    103 }
    104