Roll android_tools support library to 25.1.0
[android_tools.git] / sdk / sources / android-23 / android / support / v4 / widget / ListViewAutoScrollHelper.java
blobb23ca23b2cc63a7de219ac441f64ad88438600ff
1 /*
2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.support.v4.widget;
19 import android.view.View;
20 import android.widget.ListView;
22 /**
23 * An implementation of {@link AutoScrollHelper} that knows how to scroll
24 * through a {@link ListView}.
26 public class ListViewAutoScrollHelper extends AutoScrollHelper {
27 private final ListView mTarget;
29 public ListViewAutoScrollHelper(ListView target) {
30 super(target);
32 mTarget = target;
35 @Override
36 public void scrollTargetBy(int deltaX, int deltaY) {
37 final ListView target = mTarget;
38 final int firstPosition = target.getFirstVisiblePosition();
39 if (firstPosition == ListView.INVALID_POSITION) {
40 return;
43 final View firstView = target.getChildAt(0);
44 if (firstView == null) {
45 return;
48 final int newTop = firstView.getTop() - deltaY;
49 target.setSelectionFromTop(firstPosition, newTop);
52 @Override
53 public boolean canTargetScrollHorizontally(int direction) {
54 // List do not scroll horizontally.
55 return false;
58 @Override
59 public boolean canTargetScrollVertically(int direction) {
60 final ListView target = mTarget;
61 final int itemCount = target.getCount();
62 if (itemCount == 0) {
63 return false;
66 final int childCount = target.getChildCount();
67 final int firstPosition = target.getFirstVisiblePosition();
68 final int lastPosition = firstPosition + childCount;
70 if (direction > 0) {
71 // Are we already showing the entire last item?
72 if (lastPosition >= itemCount) {
73 final View lastView = target.getChildAt(childCount - 1);
74 if (lastView.getBottom() <= target.getHeight()) {
75 return false;
78 } else if (direction < 0) {
79 // Are we already showing the entire first item?
80 if (firstPosition <= 0) {
81 final View firstView = target.getChildAt(0);
82 if (firstView.getTop() >= 0) {
83 return false;
86 } else {
87 // The behavior for direction 0 is undefined and we can return
88 // whatever we want.
89 return false;
92 return true;