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