• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 android.support.v4.widget;
18 
19 import android.os.Build;
20 import android.support.annotation.NonNull;
21 import android.view.View;
22 import android.widget.ListView;
23 
24 /**
25  * Helper for accessing features in {@link ListView}
26  */
27 public final class ListViewCompat {
28 
29     /**
30      * Scrolls the list items within the view by a specified number of pixels.
31      *
32      * @param listView the list to scroll
33      * @param y the amount of pixels to scroll by vertically
34      */
scrollListBy(@onNull ListView listView, int y)35     public static void scrollListBy(@NonNull ListView listView, int y) {
36         if (Build.VERSION.SDK_INT >= 19) {
37             // Call the framework version directly
38             listView.scrollListBy(y);
39         } else {
40             // provide backport on earlier versions
41             final int firstPosition = listView.getFirstVisiblePosition();
42             if (firstPosition == ListView.INVALID_POSITION) {
43                 return;
44             }
45 
46             final View firstView = listView.getChildAt(0);
47             if (firstView == null) {
48                 return;
49             }
50 
51             final int newTop = firstView.getTop() - y;
52             listView.setSelectionFromTop(firstPosition, newTop);
53         }
54     }
55 
56     /**
57      * Check if the items in the list can be scrolled in a certain direction.
58      *
59      * @param direction Negative to check scrolling up, positive to check
60      *            scrolling down.
61      * @return true if the list can be scrolled in the specified direction,
62      *         false otherwise.
63      * @see #scrollListBy(ListView, int)
64      */
canScrollList(@onNull ListView listView, int direction)65     public static boolean canScrollList(@NonNull ListView listView, int direction) {
66         if (Build.VERSION.SDK_INT >= 19) {
67             // Call the framework version directly
68             return listView.canScrollList(direction);
69         } else {
70             // provide backport on earlier versions
71             final int childCount = listView.getChildCount();
72             if (childCount == 0) {
73                 return false;
74             }
75 
76             final int firstPosition = listView.getFirstVisiblePosition();
77             if (direction > 0) {
78                 final int lastBottom = listView.getChildAt(childCount - 1).getBottom();
79                 final int lastPosition = firstPosition + childCount;
80                 return lastPosition < listView.getCount()
81                         || (lastBottom > listView.getHeight() - listView.getListPaddingBottom());
82             } else {
83                 final int firstTop = listView.getChildAt(0).getTop();
84                 return firstPosition > 0 || firstTop < listView.getListPaddingTop();
85             }
86         }
87     }
88 
ListViewCompat()89     private ListViewCompat() {}
90 }
91