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.widget.ListView;
21
22/**
23 * An implementation of {@link AutoScrollHelper} that knows how to scroll
24 * through a {@link ListView}.
25 */
26public class ListViewAutoScrollHelper extends AutoScrollHelper {
27    private final ListView mTarget;
28
29    public ListViewAutoScrollHelper(ListView target) {
30        super(target);
31
32        mTarget = target;
33    }
34
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;
41        }
42
43        final View firstView = target.getChildAt(0);
44        if (firstView == null) {
45            return;
46        }
47
48        final int newTop = firstView.getTop() - deltaY;
49        target.setSelectionFromTop(firstPosition, newTop);
50    }
51
52    @Override
53    public boolean canTargetScrollHorizontally(int direction) {
54        // List do not scroll horizontally.
55        return false;
56    }
57
58    @Override
59    public boolean canTargetScrollVertically(int direction) {
60        final ListView target = mTarget;
61        final int itemCount = target.getCount();
62        final int childCount = target.getChildCount();
63        final int firstPosition = target.getFirstVisiblePosition();
64        final int lastPosition = firstPosition + childCount;
65
66        if (direction > 0) {
67            // Are we already showing the entire last item?
68            if (lastPosition >= itemCount) {
69                final View lastView = target.getChildAt(childCount - 1);
70                if (lastView.getBottom() <= target.getHeight()) {
71                    return false;
72                }
73            }
74        } else if (direction < 0) {
75            // Are we already showing the entire first item?
76            if (firstPosition <= 0) {
77                final View firstView = target.getChildAt(0);
78                if (firstView.getTop() >= 0) {
79                    return false;
80                }
81            }
82        } else {
83            // The behavior for direction 0 is undefined and we can return
84            // whatever we want.
85            return false;
86        }
87
88        return true;
89    }
90}
91