ListPopupWindowCompat.java revision 379312ec6d3e517f8bb8fcf2e9876b42f9495df3
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 17package android.support.v4.widget; 18 19import android.view.View; 20import android.view.View.OnTouchListener; 21 22/** 23 * Helper for accessing features in ListPopupWindow introduced after API level 4 24 * in a backwards compatible fashion. 25 */ 26public class ListPopupWindowCompat { 27 /** 28 * Interface for the full API. 29 */ 30 interface ListPopupWindowImpl { 31 public OnTouchListener createDragToOpenListener(Object listPopupWindow, View src); 32 } 33 34 /** 35 * Interface implementation that doesn't use anything above v4 APIs. 36 */ 37 static class BaseListPopupWindowImpl implements ListPopupWindowImpl { 38 @Override 39 public OnTouchListener createDragToOpenListener(Object listPopupWindow, View src) { 40 return null; 41 } 42 } 43 44 /** 45 * Interface implementation for devices with at least KitKat APIs. 46 */ 47 static class KitKatListPopupWindowImpl extends BaseListPopupWindowImpl { 48 @Override 49 public OnTouchListener createDragToOpenListener(Object listPopupWindow, View src) { 50 return ListPopupWindowCompatKitKat.createDragToOpenListener(listPopupWindow, src); 51 } 52 } 53 54 /** 55 * Select the correct implementation to use for the current platform. 56 */ 57 static final ListPopupWindowImpl IMPL; 58 static { 59 final int version = android.os.Build.VERSION.SDK_INT; 60 if (version >= 19) { 61 IMPL = new KitKatListPopupWindowImpl(); 62 } else { 63 IMPL = new BaseListPopupWindowImpl(); 64 } 65 } 66 67 private ListPopupWindowCompat() { 68 // This class is not publicly instantiable. 69 } 70 71 /** 72 * On API {@link android.os.Build.VERSION_CODES#KITKAT} and higher, returns 73 * an {@link OnTouchListener} that can be added to the source view to 74 * implement drag-to-open behavior. Generally, the source view should be the 75 * same view that was passed to ListPopupWindow.setAnchorView(View). 76 * <p> 77 * When the listener is set on a view, touching that view and dragging 78 * outside of its bounds will open the popup window. Lifting will select the 79 * currently touched list item. 80 * <p> 81 * Example usage: 82 * 83 * <pre> 84 * ListPopupWindow myPopup = new ListPopupWindow(context); 85 * myPopup.setAnchor(myAnchor); 86 * OnTouchListener dragListener = myPopup.createDragToOpenListener(myAnchor); 87 * myAnchor.setOnTouchListener(dragListener); 88 * </pre> 89 * 90 * @param listPopupWindow the ListPopupWindow against which to invoke the 91 * method 92 * @param src the view on which the resulting listener will be set 93 * @return a touch listener that controls drag-to-open behavior, or null on 94 * unsupported APIs 95 */ 96 public static OnTouchListener createDragToOpenListener(Object listPopupWindow, View src) { 97 return IMPL.createDragToOpenListener(listPopupWindow, src); 98 } 99} 100