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        if (itemCount == 0) {
63            return false;
64        }
65
66        final int childCount = target.getChildCount();
67        final int firstPosition = target.getFirstVisiblePosition();
68        final int lastPosition = firstPosition + childCount;
69
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;
76                }
77            }
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;
84                }
85            }
86        } else {
87            // The behavior for direction 0 is undefined and we can return
88            // whatever we want.
89            return false;
90        }
91
92        return true;
93    }
94}
95