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
;
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
) {
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
) {
43 final View firstView
= target
.getChildAt(0);
44 if (firstView
== null) {
48 final int newTop
= firstView
.getTop() - deltaY
;
49 target
.setSelectionFromTop(firstPosition
, newTop
);
53 public boolean canTargetScrollHorizontally(int direction
) {
54 // List do not scroll horizontally.
59 public boolean canTargetScrollVertically(int direction
) {
60 final ListView target
= mTarget
;
61 final int itemCount
= target
.getCount();
66 final int childCount
= target
.getChildCount();
67 final int firstPosition
= target
.getFirstVisiblePosition();
68 final int lastPosition
= firstPosition
+ childCount
;
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()) {
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) {
87 // The behavior for direction 0 is undefined and we can return