ScrollView.java revision f116bf8884b5b58aae261d148003811aa4a7c6e9
1/*
2 * Copyright (C) 2006 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.widget;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.Rect;
22import android.util.AttributeSet;
23import android.view.FocusFinder;
24import android.view.KeyEvent;
25import android.view.MotionEvent;
26import android.view.VelocityTracker;
27import android.view.View;
28import android.view.ViewConfiguration;
29import android.view.ViewGroup;
30import android.view.ViewParent;
31import android.view.animation.AnimationUtils;
32
33import com.android.internal.R;
34
35import java.util.List;
36
37/**
38 * Layout container for a view hierarchy that can be scrolled by the user,
39 * allowing it to be larger than the physical display.  A ScrollView
40 * is a {@link FrameLayout}, meaning you should place one child in it
41 * containing the entire contents to scroll; this child may itself be a layout
42 * manager with a complex hierarchy of objects.  A child that is often used
43 * is a {@link LinearLayout} in a vertical orientation, presenting a vertical
44 * array of top-level items that the user can scroll through.
45 *
46 * <p>The {@link TextView} class also
47 * takes care of its own scrolling, so does not require a ScrollView, but
48 * using the two together is possible to achieve the effect of a text view
49 * within a larger container.
50 *
51 * <p>ScrollView only supports vertical scrolling.
52 */
53public class ScrollView extends FrameLayout {
54    static final String TAG = "ScrollView";
55
56    static final int ANIMATED_SCROLL_GAP = 250;
57
58    static final float MAX_SCROLL_FACTOR = 0.5f;
59
60
61    private long mLastScroll;
62
63    private final Rect mTempRect = new Rect();
64    private Scroller mScroller;
65
66    /**
67     * Flag to indicate that we are moving focus ourselves. This is so the
68     * code that watches for focus changes initiated outside this ScrollView
69     * knows that it does not have to do anything.
70     */
71    private boolean mScrollViewMovedFocus;
72
73    /**
74     * Position of the last motion event.
75     */
76    private float mLastMotionY;
77
78    /**
79     * True when the layout has changed but the traversal has not come through yet.
80     * Ideally the view hierarchy would keep track of this for us.
81     */
82    private boolean mIsLayoutDirty = true;
83
84    /**
85     * The child to give focus to in the event that a child has requested focus while the
86     * layout is dirty. This prevents the scroll from being wrong if the child has not been
87     * laid out before requesting focus.
88     */
89    private View mChildToScrollTo = null;
90
91    /**
92     * True if the user is currently dragging this ScrollView around. This is
93     * not the same as 'is being flinged', which can be checked by
94     * mScroller.isFinished() (flinging begins when the user lifts his finger).
95     */
96    private boolean mIsBeingDragged = false;
97
98    /**
99     * Determines speed during touch scrolling
100     */
101    private VelocityTracker mVelocityTracker;
102
103    /**
104     * When set to true, the scroll view measure its child to make it fill the currently
105     * visible area.
106     */
107    private boolean mFillViewport;
108
109    /**
110     * Whether arrow scrolling is animated.
111     */
112    private boolean mSmoothScrollingEnabled = true;
113
114    private int mTouchSlop;
115    private int mMinimumVelocity;
116    private int mMaximumVelocity;
117
118    public ScrollView(Context context) {
119        this(context, null);
120    }
121
122    public ScrollView(Context context, AttributeSet attrs) {
123        this(context, attrs, com.android.internal.R.attr.scrollViewStyle);
124    }
125
126    public ScrollView(Context context, AttributeSet attrs, int defStyle) {
127        super(context, attrs, defStyle);
128        initScrollView();
129
130        TypedArray a =
131            context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.ScrollView, defStyle, 0);
132
133        setFillViewport(a.getBoolean(R.styleable.ScrollView_fillViewport, false));
134
135        a.recycle();
136    }
137
138    @Override
139    protected float getTopFadingEdgeStrength() {
140        if (getChildCount() == 0) {
141            return 0.0f;
142        }
143
144        final int length = getVerticalFadingEdgeLength();
145        if (mScrollY < length) {
146            return mScrollY / (float) length;
147        }
148
149        return 1.0f;
150    }
151
152    @Override
153    protected float getBottomFadingEdgeStrength() {
154        if (getChildCount() == 0) {
155            return 0.0f;
156        }
157
158        final int length = getVerticalFadingEdgeLength();
159        final int bottomEdge = getHeight() - mPaddingBottom;
160        final int span = getChildAt(0).getBottom() - mScrollY - bottomEdge;
161        if (span < length) {
162            return span / (float) length;
163        }
164
165        return 1.0f;
166    }
167
168    /**
169     * @return The maximum amount this scroll view will scroll in response to
170     *   an arrow event.
171     */
172    public int getMaxScrollAmount() {
173        return (int) (MAX_SCROLL_FACTOR * (mBottom - mTop));
174    }
175
176
177    private void initScrollView() {
178        mScroller = new Scroller(getContext());
179        setFocusable(true);
180        setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
181        setWillNotDraw(false);
182        final ViewConfiguration configuration = ViewConfiguration.get(mContext);
183        mTouchSlop = configuration.getScaledTouchSlop();
184        mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
185        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
186    }
187
188    @Override
189    public void addView(View child) {
190        if (getChildCount() > 0) {
191            throw new IllegalStateException("ScrollView can host only one direct child");
192        }
193
194        super.addView(child);
195    }
196
197    @Override
198    public void addView(View child, int index) {
199        if (getChildCount() > 0) {
200            throw new IllegalStateException("ScrollView can host only one direct child");
201        }
202
203        super.addView(child, index);
204    }
205
206    @Override
207    public void addView(View child, ViewGroup.LayoutParams params) {
208        if (getChildCount() > 0) {
209            throw new IllegalStateException("ScrollView can host only one direct child");
210        }
211
212        super.addView(child, params);
213    }
214
215    @Override
216    public void addView(View child, int index, ViewGroup.LayoutParams params) {
217        if (getChildCount() > 0) {
218            throw new IllegalStateException("ScrollView can host only one direct child");
219        }
220
221        super.addView(child, index, params);
222    }
223
224    /**
225     * @return Returns true this ScrollView can be scrolled
226     */
227    private boolean canScroll() {
228        View child = getChildAt(0);
229        if (child != null) {
230            int childHeight = child.getHeight();
231            return getHeight() < childHeight + mPaddingTop + mPaddingBottom;
232        }
233        return false;
234    }
235
236    /**
237     * Indicates whether this ScrollView's content is stretched to fill the viewport.
238     *
239     * @return True if the content fills the viewport, false otherwise.
240     */
241    public boolean isFillViewport() {
242        return mFillViewport;
243    }
244
245    /**
246     * Indicates this ScrollView whether it should stretch its content height to fill
247     * the viewport or not.
248     *
249     * @param fillViewport True to stretch the content's height to the viewport's
250     *        boundaries, false otherwise.
251     */
252    public void setFillViewport(boolean fillViewport) {
253        if (fillViewport != mFillViewport) {
254            mFillViewport = fillViewport;
255            requestLayout();
256        }
257    }
258
259    /**
260     * @return Whether arrow scrolling will animate its transition.
261     */
262    public boolean isSmoothScrollingEnabled() {
263        return mSmoothScrollingEnabled;
264    }
265
266    /**
267     * Set whether arrow scrolling will animate its transition.
268     * @param smoothScrollingEnabled whether arrow scrolling will animate its transition
269     */
270    public void setSmoothScrollingEnabled(boolean smoothScrollingEnabled) {
271        mSmoothScrollingEnabled = smoothScrollingEnabled;
272    }
273
274    @Override
275    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
276        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
277
278        if (!mFillViewport) {
279            return;
280        }
281
282        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
283        if (heightMode == MeasureSpec.UNSPECIFIED) {
284            return;
285        }
286
287        if (getChildCount() > 0) {
288            final View child = getChildAt(0);
289            int height = getMeasuredHeight();
290            if (child.getMeasuredHeight() < height) {
291                final FrameLayout.LayoutParams lp = (LayoutParams) child.getLayoutParams();
292
293                int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, mPaddingLeft
294                        + mPaddingRight, lp.width);
295                height -= mPaddingTop;
296                height -= mPaddingBottom;
297                int childHeightMeasureSpec =
298                        MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
299
300                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
301            }
302        }
303    }
304
305    @Override
306    public boolean dispatchKeyEvent(KeyEvent event) {
307        // Let the focused view and/or our descendants get the key first
308        boolean handled = super.dispatchKeyEvent(event);
309        if (handled) {
310            return true;
311        }
312        return executeKeyEvent(event);
313    }
314
315    /**
316     * You can call this function yourself to have the scroll view perform
317     * scrolling from a key event, just as if the event had been dispatched to
318     * it by the view hierarchy.
319     *
320     * @param event The key event to execute.
321     * @return Return true if the event was handled, else false.
322     */
323    public boolean executeKeyEvent(KeyEvent event) {
324        mTempRect.setEmpty();
325
326        if (!canScroll()) {
327            if (isFocused()) {
328                View currentFocused = findFocus();
329                if (currentFocused == this) currentFocused = null;
330                View nextFocused = FocusFinder.getInstance().findNextFocus(this,
331                        currentFocused, View.FOCUS_DOWN);
332                return nextFocused != null
333                        && nextFocused != this
334                        && nextFocused.requestFocus(View.FOCUS_DOWN);
335            }
336            return false;
337        }
338
339        boolean handled = false;
340        if (event.getAction() == KeyEvent.ACTION_DOWN) {
341            switch (event.getKeyCode()) {
342                case KeyEvent.KEYCODE_DPAD_UP:
343                    if (!event.isAltPressed()) {
344                        handled = arrowScroll(View.FOCUS_UP);
345                    } else {
346                        handled = fullScroll(View.FOCUS_UP);
347                    }
348                    break;
349                case KeyEvent.KEYCODE_DPAD_DOWN:
350                    if (!event.isAltPressed()) {
351                        handled = arrowScroll(View.FOCUS_DOWN);
352                    } else {
353                        handled = fullScroll(View.FOCUS_DOWN);
354                    }
355                    break;
356                case KeyEvent.KEYCODE_SPACE:
357                    pageScroll(event.isShiftPressed() ? View.FOCUS_UP : View.FOCUS_DOWN);
358                    break;
359            }
360        }
361
362        return handled;
363    }
364
365    @Override
366    public boolean onInterceptTouchEvent(MotionEvent ev) {
367        /*
368         * This method JUST determines whether we want to intercept the motion.
369         * If we return true, onMotionEvent will be called and we do the actual
370         * scrolling there.
371         */
372
373        /*
374        * Shortcut the most recurring case: the user is in the dragging
375        * state and he is moving his finger.  We want to intercept this
376        * motion.
377        */
378        final int action = ev.getAction();
379        if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) {
380            return true;
381        }
382
383        if (!canScroll()) {
384            mIsBeingDragged = false;
385            return false;
386        }
387
388        final float y = ev.getY();
389
390        switch (action) {
391            case MotionEvent.ACTION_MOVE:
392                /*
393                 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
394                 * whether the user has moved far enough from his original down touch.
395                 */
396
397                /*
398                * Locally do absolute value. mLastMotionY is set to the y value
399                * of the down event.
400                */
401                final int yDiff = (int) Math.abs(y - mLastMotionY);
402                if (yDiff > mTouchSlop) {
403                    mIsBeingDragged = true;
404                }
405                break;
406
407            case MotionEvent.ACTION_DOWN:
408                /* Remember location of down touch */
409                mLastMotionY = y;
410
411                /*
412                * If being flinged and user touches the screen, initiate drag;
413                * otherwise don't.  mScroller.isFinished should be false when
414                * being flinged.
415                */
416                mIsBeingDragged = !mScroller.isFinished();
417                break;
418
419            case MotionEvent.ACTION_CANCEL:
420            case MotionEvent.ACTION_UP:
421                /* Release the drag */
422                mIsBeingDragged = false;
423                break;
424        }
425
426        /*
427        * The only time we want to intercept motion events is if we are in the
428        * drag mode.
429        */
430        return mIsBeingDragged;
431    }
432
433    @Override
434    public boolean onTouchEvent(MotionEvent ev) {
435
436        if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
437            // Don't handle edge touches immediately -- they may actually belong to one of our
438            // descendants.
439            return false;
440        }
441
442        if (!canScroll()) {
443            return false;
444        }
445
446        if (mVelocityTracker == null) {
447            mVelocityTracker = VelocityTracker.obtain();
448        }
449        mVelocityTracker.addMovement(ev);
450
451        final int action = ev.getAction();
452        final float y = ev.getY();
453
454        switch (action) {
455            case MotionEvent.ACTION_DOWN:
456                /*
457                * If being flinged and user touches, stop the fling. isFinished
458                * will be false if being flinged.
459                */
460                if (!mScroller.isFinished()) {
461                    mScroller.abortAnimation();
462                }
463
464                // Remember where the motion event started
465                mLastMotionY = y;
466                break;
467            case MotionEvent.ACTION_MOVE:
468                // Scroll to follow the motion event
469                final int deltaY = (int) (mLastMotionY - y);
470                mLastMotionY = y;
471
472                if (deltaY < 0) {
473                    if (mScrollY > 0) {
474                        scrollBy(0, deltaY);
475                    }
476                } else if (deltaY > 0) {
477                    final int bottomEdge = getHeight() - mPaddingBottom;
478                    final int availableToScroll = getChildAt(0).getBottom() - mScrollY - bottomEdge;
479                    if (availableToScroll > 0) {
480                        scrollBy(0, Math.min(availableToScroll, deltaY));
481                    }
482                }
483                break;
484            case MotionEvent.ACTION_UP:
485                final VelocityTracker velocityTracker = mVelocityTracker;
486                velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
487                int initialVelocity = (int) velocityTracker.getYVelocity();
488
489                if ((Math.abs(initialVelocity) > mMinimumVelocity) && getChildCount() > 0) {
490                    fling(-initialVelocity);
491                }
492
493                if (mVelocityTracker != null) {
494                    mVelocityTracker.recycle();
495                    mVelocityTracker = null;
496                }
497        }
498        return true;
499    }
500
501    /**
502     * <p>
503     * Finds the next focusable component that fits in this View's bounds
504     * (excluding fading edges) pretending that this View's top is located at
505     * the parameter top.
506     * </p>
507     *
508     * @param topFocus           look for a candidate is the one at the top of the bounds
509     *                           if topFocus is true, or at the bottom of the bounds if topFocus is
510     *                           false
511     * @param top                the top offset of the bounds in which a focusable must be
512     *                           found (the fading edge is assumed to start at this position)
513     * @param preferredFocusable the View that has highest priority and will be
514     *                           returned if it is within my bounds (null is valid)
515     * @return the next focusable component in the bounds or null if none can be
516     *         found
517     */
518    private View findFocusableViewInMyBounds(final boolean topFocus,
519            final int top, View preferredFocusable) {
520        /*
521         * The fading edge's transparent side should be considered for focus
522         * since it's mostly visible, so we divide the actual fading edge length
523         * by 2.
524         */
525        final int fadingEdgeLength = getVerticalFadingEdgeLength() / 2;
526        final int topWithoutFadingEdge = top + fadingEdgeLength;
527        final int bottomWithoutFadingEdge = top + getHeight() - fadingEdgeLength;
528
529        if ((preferredFocusable != null)
530                && (preferredFocusable.getTop() < bottomWithoutFadingEdge)
531                && (preferredFocusable.getBottom() > topWithoutFadingEdge)) {
532            return preferredFocusable;
533        }
534
535        return findFocusableViewInBounds(topFocus, topWithoutFadingEdge,
536                bottomWithoutFadingEdge);
537    }
538
539    /**
540     * <p>
541     * Finds the next focusable component that fits in the specified bounds.
542     * </p>
543     *
544     * @param topFocus look for a candidate is the one at the top of the bounds
545     *                 if topFocus is true, or at the bottom of the bounds if topFocus is
546     *                 false
547     * @param top      the top offset of the bounds in which a focusable must be
548     *                 found
549     * @param bottom   the bottom offset of the bounds in which a focusable must
550     *                 be found
551     * @return the next focusable component in the bounds or null if none can
552     *         be found
553     */
554    private View findFocusableViewInBounds(boolean topFocus, int top, int bottom) {
555
556        List<View> focusables = getFocusables(View.FOCUS_FORWARD);
557        View focusCandidate = null;
558
559        /*
560         * A fully contained focusable is one where its top is below the bound's
561         * top, and its bottom is above the bound's bottom. A partially
562         * contained focusable is one where some part of it is within the
563         * bounds, but it also has some part that is not within bounds.  A fully contained
564         * focusable is preferred to a partially contained focusable.
565         */
566        boolean foundFullyContainedFocusable = false;
567
568        int count = focusables.size();
569        for (int i = 0; i < count; i++) {
570            View view = focusables.get(i);
571            int viewTop = view.getTop();
572            int viewBottom = view.getBottom();
573
574            if (top < viewBottom && viewTop < bottom) {
575                /*
576                 * the focusable is in the target area, it is a candidate for
577                 * focusing
578                 */
579
580                final boolean viewIsFullyContained = (top < viewTop) &&
581                        (viewBottom < bottom);
582
583                if (focusCandidate == null) {
584                    /* No candidate, take this one */
585                    focusCandidate = view;
586                    foundFullyContainedFocusable = viewIsFullyContained;
587                } else {
588                    final boolean viewIsCloserToBoundary =
589                            (topFocus && viewTop < focusCandidate.getTop()) ||
590                                    (!topFocus && viewBottom > focusCandidate
591                                            .getBottom());
592
593                    if (foundFullyContainedFocusable) {
594                        if (viewIsFullyContained && viewIsCloserToBoundary) {
595                            /*
596                             * We're dealing with only fully contained views, so
597                             * it has to be closer to the boundary to beat our
598                             * candidate
599                             */
600                            focusCandidate = view;
601                        }
602                    } else {
603                        if (viewIsFullyContained) {
604                            /* Any fully contained view beats a partially contained view */
605                            focusCandidate = view;
606                            foundFullyContainedFocusable = true;
607                        } else if (viewIsCloserToBoundary) {
608                            /*
609                             * Partially contained view beats another partially
610                             * contained view if it's closer
611                             */
612                            focusCandidate = view;
613                        }
614                    }
615                }
616            }
617        }
618
619        return focusCandidate;
620    }
621
622    /**
623     * <p>Handles scrolling in response to a "page up/down" shortcut press. This
624     * method will scroll the view by one page up or down and give the focus
625     * to the topmost/bottommost component in the new visible area. If no
626     * component is a good candidate for focus, this scrollview reclaims the
627     * focus.</p>
628     *
629     * @param direction the scroll direction: {@link android.view.View#FOCUS_UP}
630     *                  to go one page up or
631     *                  {@link android.view.View#FOCUS_DOWN} to go one page down
632     * @return true if the key event is consumed by this method, false otherwise
633     */
634    public boolean pageScroll(int direction) {
635        boolean down = direction == View.FOCUS_DOWN;
636        int height = getHeight();
637
638        if (down) {
639            mTempRect.top = getScrollY() + height;
640            int count = getChildCount();
641            if (count > 0) {
642                View view = getChildAt(count - 1);
643                if (mTempRect.top + height > view.getBottom()) {
644                    mTempRect.top = view.getBottom() - height;
645                }
646            }
647        } else {
648            mTempRect.top = getScrollY() - height;
649            if (mTempRect.top < 0) {
650                mTempRect.top = 0;
651            }
652        }
653        mTempRect.bottom = mTempRect.top + height;
654
655        return scrollAndFocus(direction, mTempRect.top, mTempRect.bottom);
656    }
657
658    /**
659     * <p>Handles scrolling in response to a "home/end" shortcut press. This
660     * method will scroll the view to the top or bottom and give the focus
661     * to the topmost/bottommost component in the new visible area. If no
662     * component is a good candidate for focus, this scrollview reclaims the
663     * focus.</p>
664     *
665     * @param direction the scroll direction: {@link android.view.View#FOCUS_UP}
666     *                  to go the top of the view or
667     *                  {@link android.view.View#FOCUS_DOWN} to go the bottom
668     * @return true if the key event is consumed by this method, false otherwise
669     */
670    public boolean fullScroll(int direction) {
671        boolean down = direction == View.FOCUS_DOWN;
672        int height = getHeight();
673
674        mTempRect.top = 0;
675        mTempRect.bottom = height;
676
677        if (down) {
678            int count = getChildCount();
679            if (count > 0) {
680                View view = getChildAt(count - 1);
681                mTempRect.bottom = view.getBottom();
682                mTempRect.top = mTempRect.bottom - height;
683            }
684        }
685
686        return scrollAndFocus(direction, mTempRect.top, mTempRect.bottom);
687    }
688
689    /**
690     * <p>Scrolls the view to make the area defined by <code>top</code> and
691     * <code>bottom</code> visible. This method attempts to give the focus
692     * to a component visible in this area. If no component can be focused in
693     * the new visible area, the focus is reclaimed by this scrollview.</p>
694     *
695     * @param direction the scroll direction: {@link android.view.View#FOCUS_UP}
696     *                  to go upward
697     *                  {@link android.view.View#FOCUS_DOWN} to downward
698     * @param top       the top offset of the new area to be made visible
699     * @param bottom    the bottom offset of the new area to be made visible
700     * @return true if the key event is consumed by this method, false otherwise
701     */
702    private boolean scrollAndFocus(int direction, int top, int bottom) {
703        boolean handled = true;
704
705        int height = getHeight();
706        int containerTop = getScrollY();
707        int containerBottom = containerTop + height;
708        boolean up = direction == View.FOCUS_UP;
709
710        View newFocused = findFocusableViewInBounds(up, top, bottom);
711        if (newFocused == null) {
712            newFocused = this;
713        }
714
715        if (top >= containerTop && bottom <= containerBottom) {
716            handled = false;
717        } else {
718            int delta = up ? (top - containerTop) : (bottom - containerBottom);
719            doScrollY(delta);
720        }
721
722        if (newFocused != findFocus() && newFocused.requestFocus(direction)) {
723            mScrollViewMovedFocus = true;
724            mScrollViewMovedFocus = false;
725        }
726
727        return handled;
728    }
729
730    /**
731     * Handle scrolling in response to an up or down arrow click.
732     *
733     * @param direction The direction corresponding to the arrow key that was
734     *                  pressed
735     * @return True if we consumed the event, false otherwise
736     */
737    public boolean arrowScroll(int direction) {
738
739        View currentFocused = findFocus();
740        if (currentFocused == this) currentFocused = null;
741
742        View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
743
744        final int maxJump = getMaxScrollAmount();
745
746        if (nextFocused != null && isWithinDeltaOfScreen(nextFocused, maxJump, getHeight())) {
747            nextFocused.getDrawingRect(mTempRect);
748            offsetDescendantRectToMyCoords(nextFocused, mTempRect);
749            int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
750            doScrollY(scrollDelta);
751            nextFocused.requestFocus(direction);
752        } else {
753            // no new focus
754            int scrollDelta = maxJump;
755
756            if (direction == View.FOCUS_UP && getScrollY() < scrollDelta) {
757                scrollDelta = getScrollY();
758            } else if (direction == View.FOCUS_DOWN) {
759                if (getChildCount() > 0) {
760                    int daBottom = getChildAt(0).getBottom();
761
762                    int screenBottom = getScrollY() + getHeight();
763
764                    if (daBottom - screenBottom < maxJump) {
765                        scrollDelta = daBottom - screenBottom;
766                    }
767                }
768            }
769            if (scrollDelta == 0) {
770                return false;
771            }
772            doScrollY(direction == View.FOCUS_DOWN ? scrollDelta : -scrollDelta);
773        }
774
775        if (currentFocused != null && currentFocused.isFocused()
776                && isOffScreen(currentFocused)) {
777            // previously focused item still has focus and is off screen, give
778            // it up (take it back to ourselves)
779            // (also, need to temporarily force FOCUS_BEFORE_DESCENDANTS so we are
780            // sure to
781            // get it)
782            final int descendantFocusability = getDescendantFocusability();  // save
783            setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
784            requestFocus();
785            setDescendantFocusability(descendantFocusability);  // restore
786        }
787        return true;
788    }
789
790    /**
791     * @return whether the descendant of this scroll view is scrolled off
792     *  screen.
793     */
794    private boolean isOffScreen(View descendant) {
795        return !isWithinDeltaOfScreen(descendant, 0, getHeight());
796    }
797
798    /**
799     * @return whether the descendant of this scroll view is within delta
800     *  pixels of being on the screen.
801     */
802    private boolean isWithinDeltaOfScreen(View descendant, int delta, int height) {
803        descendant.getDrawingRect(mTempRect);
804        offsetDescendantRectToMyCoords(descendant, mTempRect);
805
806        return (mTempRect.bottom + delta) >= getScrollY()
807                && (mTempRect.top - delta) <= (getScrollY() + height);
808    }
809
810    /**
811     * Smooth scroll by a Y delta
812     *
813     * @param delta the number of pixels to scroll by on the Y axis
814     */
815    private void doScrollY(int delta) {
816        if (delta != 0) {
817            if (mSmoothScrollingEnabled) {
818                smoothScrollBy(0, delta);
819            } else {
820                scrollBy(0, delta);
821            }
822        }
823    }
824
825    /**
826     * Like {@link View#scrollBy}, but scroll smoothly instead of immediately.
827     *
828     * @param dx the number of pixels to scroll by on the X axis
829     * @param dy the number of pixels to scroll by on the Y axis
830     */
831    public final void smoothScrollBy(int dx, int dy) {
832        long duration = AnimationUtils.currentAnimationTimeMillis() - mLastScroll;
833        if (duration > ANIMATED_SCROLL_GAP) {
834            mScroller.startScroll(mScrollX, mScrollY, dx, dy);
835            awakenScrollBars(mScroller.getDuration());
836            invalidate();
837        } else {
838            if (!mScroller.isFinished()) {
839                mScroller.abortAnimation();
840            }
841            scrollBy(dx, dy);
842        }
843        mLastScroll = AnimationUtils.currentAnimationTimeMillis();
844    }
845
846    /**
847     * Like {@link #scrollTo}, but scroll smoothly instead of immediately.
848     *
849     * @param x the position where to scroll on the X axis
850     * @param y the position where to scroll on the Y axis
851     */
852    public final void smoothScrollTo(int x, int y) {
853        smoothScrollBy(x - mScrollX, y - mScrollY);
854    }
855
856    /**
857     * <p>The scroll range of a scroll view is the overall height of all of its
858     * children.</p>
859     */
860    @Override
861    protected int computeVerticalScrollRange() {
862        int count = getChildCount();
863        return count == 0 ? getHeight() : (getChildAt(0)).getBottom();
864    }
865
866
867    @Override
868    protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
869        ViewGroup.LayoutParams lp = child.getLayoutParams();
870
871        int childWidthMeasureSpec;
872        int childHeightMeasureSpec;
873
874        childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, mPaddingLeft
875                + mPaddingRight, lp.width);
876
877        childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
878
879        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
880    }
881
882    @Override
883    protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
884            int parentHeightMeasureSpec, int heightUsed) {
885        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
886
887        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
888                mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
889                        + widthUsed, lp.width);
890        final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
891                lp.topMargin + lp.bottomMargin, MeasureSpec.UNSPECIFIED);
892
893        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
894    }
895
896    @Override
897    public void computeScroll() {
898        if (mScroller.computeScrollOffset()) {
899            // This is called at drawing time by ViewGroup.  We don't want to
900            // re-show the scrollbars at this point, which scrollTo will do,
901            // so we replicate most of scrollTo here.
902            //
903            //         It's a little odd to call onScrollChanged from inside the drawing.
904            //
905            //         It is, except when you remember that computeScroll() is used to
906            //         animate scrolling. So unless we want to defer the onScrollChanged()
907            //         until the end of the animated scrolling, we don't really have a
908            //         choice here.
909            //
910            //         I agree.  The alternative, which I think would be worse, is to post
911            //         something and tell the subclasses later.  This is bad because there
912            //         will be a window where mScrollX/Y is different from what the app
913            //         thinks it is.
914            //
915            int oldX = mScrollX;
916            int oldY = mScrollY;
917            int x = mScroller.getCurrX();
918            int y = mScroller.getCurrY();
919            if (getChildCount() > 0) {
920                View child = getChildAt(0);
921                mScrollX = clamp(x, getWidth() - mPaddingRight - mPaddingLeft, child.getWidth());
922                mScrollY = clamp(y, getHeight() - mPaddingBottom - mPaddingTop, child.getHeight());
923            } else {
924                mScrollX = x;
925                mScrollY = y;
926            }
927            if (oldX != mScrollX || oldY != mScrollY) {
928                onScrollChanged(mScrollX, mScrollY, oldX, oldY);
929            }
930
931            // Keep on drawing until the animation has finished.
932            postInvalidate();
933        }
934    }
935
936    /**
937     * Scrolls the view to the given child.
938     *
939     * @param child the View to scroll to
940     */
941    private void scrollToChild(View child) {
942        child.getDrawingRect(mTempRect);
943
944        /* Offset from child's local coordinates to ScrollView coordinates */
945        offsetDescendantRectToMyCoords(child, mTempRect);
946
947        int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
948
949        if (scrollDelta != 0) {
950            scrollBy(0, scrollDelta);
951        }
952    }
953
954    /**
955     * If rect is off screen, scroll just enough to get it (or at least the
956     * first screen size chunk of it) on screen.
957     *
958     * @param rect      The rectangle.
959     * @param immediate True to scroll immediately without animation
960     * @return true if scrolling was performed
961     */
962    private boolean scrollToChildRect(Rect rect, boolean immediate) {
963        final int delta = computeScrollDeltaToGetChildRectOnScreen(rect);
964        final boolean scroll = delta != 0;
965        if (scroll) {
966            if (immediate) {
967                scrollBy(0, delta);
968            } else {
969                smoothScrollBy(0, delta);
970            }
971        }
972        return scroll;
973    }
974
975    /**
976     * Compute the amount to scroll in the Y direction in order to get
977     * a rectangle completely on the screen (or, if taller than the screen,
978     * at least the first screen size chunk of it).
979     *
980     * @param rect The rect.
981     * @return The scroll delta.
982     */
983    protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
984        if (getChildCount() == 0) return 0;
985
986        int height = getHeight();
987        int screenTop = getScrollY();
988        int screenBottom = screenTop + height;
989
990        int fadingEdge = getVerticalFadingEdgeLength();
991
992        // leave room for top fading edge as long as rect isn't at very top
993        if (rect.top > 0) {
994            screenTop += fadingEdge;
995        }
996
997        // leave room for bottom fading edge as long as rect isn't at very bottom
998        if (rect.bottom < getChildAt(0).getHeight()) {
999            screenBottom -= fadingEdge;
1000        }
1001
1002        int scrollYDelta = 0;
1003
1004        if (rect.bottom > screenBottom && rect.top > screenTop) {
1005            // need to move down to get it in view: move down just enough so
1006            // that the entire rectangle is in view (or at least the first
1007            // screen size chunk).
1008
1009            if (rect.height() > height) {
1010                // just enough to get screen size chunk on
1011                scrollYDelta += (rect.top - screenTop);
1012            } else {
1013                // get entire rect at bottom of screen
1014                scrollYDelta += (rect.bottom - screenBottom);
1015            }
1016
1017            // make sure we aren't scrolling beyond the end of our content
1018            int bottom = getChildAt(0).getBottom();
1019            int distanceToBottom = bottom - screenBottom;
1020            scrollYDelta = Math.min(scrollYDelta, distanceToBottom);
1021
1022        } else if (rect.top < screenTop && rect.bottom < screenBottom) {
1023            // need to move up to get it in view: move up just enough so that
1024            // entire rectangle is in view (or at least the first screen
1025            // size chunk of it).
1026
1027            if (rect.height() > height) {
1028                // screen size chunk
1029                scrollYDelta -= (screenBottom - rect.bottom);
1030            } else {
1031                // entire rect at top
1032                scrollYDelta -= (screenTop - rect.top);
1033            }
1034
1035            // make sure we aren't scrolling any further than the top our content
1036            scrollYDelta = Math.max(scrollYDelta, -getScrollY());
1037        }
1038        return scrollYDelta;
1039    }
1040
1041    @Override
1042    public void requestChildFocus(View child, View focused) {
1043        if (!mScrollViewMovedFocus) {
1044            if (!mIsLayoutDirty) {
1045                scrollToChild(focused);
1046            } else {
1047                // The child may not be laid out yet, we can't compute the scroll yet
1048                mChildToScrollTo = focused;
1049            }
1050        }
1051        super.requestChildFocus(child, focused);
1052    }
1053
1054
1055    /**
1056     * When looking for focus in children of a scroll view, need to be a little
1057     * more careful not to give focus to something that is scrolled off screen.
1058     *
1059     * This is more expensive than the default {@link android.view.ViewGroup}
1060     * implementation, otherwise this behavior might have been made the default.
1061     */
1062    @Override
1063    protected boolean onRequestFocusInDescendants(int direction,
1064            Rect previouslyFocusedRect) {
1065
1066        // convert from forward / backward notation to up / down / left / right
1067        // (ugh).
1068        if (direction == View.FOCUS_FORWARD) {
1069            direction = View.FOCUS_DOWN;
1070        } else if (direction == View.FOCUS_BACKWARD) {
1071            direction = View.FOCUS_UP;
1072        }
1073
1074        final View nextFocus = previouslyFocusedRect == null ?
1075                FocusFinder.getInstance().findNextFocus(this, null, direction) :
1076                FocusFinder.getInstance().findNextFocusFromRect(this,
1077                        previouslyFocusedRect, direction);
1078
1079        if (nextFocus == null) {
1080            return false;
1081        }
1082
1083        if (isOffScreen(nextFocus)) {
1084            return false;
1085        }
1086
1087        return nextFocus.requestFocus(direction, previouslyFocusedRect);
1088    }
1089
1090    @Override
1091    public boolean requestChildRectangleOnScreen(View child, Rect rectangle,
1092            boolean immediate) {
1093        // offset into coordinate space of this scroll view
1094        rectangle.offset(child.getLeft() - child.getScrollX(),
1095                child.getTop() - child.getScrollY());
1096
1097        return scrollToChildRect(rectangle, immediate);
1098    }
1099
1100    @Override
1101    public void requestLayout() {
1102        mIsLayoutDirty = true;
1103        super.requestLayout();
1104    }
1105
1106    @Override
1107    protected void onLayout(boolean changed, int l, int t, int r, int b) {
1108        super.onLayout(changed, l, t, r, b);
1109        mIsLayoutDirty = false;
1110        // Give a child focus if it needs it
1111        if (mChildToScrollTo != null && isViewDescendantOf(mChildToScrollTo, this)) {
1112                scrollToChild(mChildToScrollTo);
1113        }
1114        mChildToScrollTo = null;
1115
1116        // Calling this with the present values causes it to re-clam them
1117        scrollTo(mScrollX, mScrollY);
1118    }
1119
1120    @Override
1121    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1122        super.onSizeChanged(w, h, oldw, oldh);
1123
1124        View currentFocused = findFocus();
1125        if (null == currentFocused || this == currentFocused)
1126            return;
1127
1128        // If the currently-focused view was visible on the screen when the
1129        // screen was at the old height, then scroll the screen to make that
1130        // view visible with the new screen height.
1131        if (isWithinDeltaOfScreen(currentFocused, 0, oldh)) {
1132            currentFocused.getDrawingRect(mTempRect);
1133            offsetDescendantRectToMyCoords(currentFocused, mTempRect);
1134            int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
1135            doScrollY(scrollDelta);
1136        }
1137    }
1138
1139    /**
1140     * Return true if child is an descendant of parent, (or equal to the parent).
1141     */
1142    private boolean isViewDescendantOf(View child, View parent) {
1143        if (child == parent) {
1144            return true;
1145        }
1146
1147        final ViewParent theParent = child.getParent();
1148        return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent);
1149    }
1150
1151    /**
1152     * Fling the scroll view
1153     *
1154     * @param velocityY The initial velocity in the Y direction. Positive
1155     *                  numbers mean that the finger/curor is moving down the screen,
1156     *                  which means we want to scroll towards the top.
1157     */
1158    public void fling(int velocityY) {
1159        if (getChildCount() > 0) {
1160            int height = getHeight() - mPaddingBottom - mPaddingTop;
1161            int bottom = getChildAt(0).getHeight();
1162
1163            mScroller.fling(mScrollX, mScrollY, 0, velocityY, 0, 0, 0, bottom - height);
1164
1165            final boolean movingDown = velocityY > 0;
1166
1167            View newFocused =
1168                    findFocusableViewInMyBounds(movingDown, mScroller.getFinalY(), findFocus());
1169            if (newFocused == null) {
1170                newFocused = this;
1171            }
1172
1173            if (newFocused != findFocus()
1174                    && newFocused.requestFocus(movingDown ? View.FOCUS_DOWN : View.FOCUS_UP)) {
1175                mScrollViewMovedFocus = true;
1176                mScrollViewMovedFocus = false;
1177            }
1178
1179            awakenScrollBars(mScroller.getDuration());
1180            invalidate();
1181        }
1182    }
1183
1184    /**
1185     * {@inheritDoc}
1186     *
1187     * <p>This version also clamps the scrolling to the bounds of our child.
1188     */
1189    public void scrollTo(int x, int y) {
1190        // we rely on the fact the View.scrollBy calls scrollTo.
1191        if (getChildCount() > 0) {
1192            View child = getChildAt(0);
1193            x = clamp(x, getWidth() - mPaddingRight - mPaddingLeft, child.getWidth());
1194            y = clamp(y, getHeight() - mPaddingBottom - mPaddingTop, child.getHeight());
1195            if (x != mScrollX || y != mScrollY) {
1196                super.scrollTo(x, y);
1197            }
1198        }
1199    }
1200
1201    private int clamp(int n, int my, int child) {
1202        if (my >= child || n < 0) {
1203            /* my >= child is this case:
1204             *                    |--------------- me ---------------|
1205             *     |------ child ------|
1206             * or
1207             *     |--------------- me ---------------|
1208             *            |------ child ------|
1209             * or
1210             *     |--------------- me ---------------|
1211             *                                  |------ child ------|
1212             *
1213             * n < 0 is this case:
1214             *     |------ me ------|
1215             *                    |-------- child --------|
1216             *     |-- mScrollX --|
1217             */
1218            return 0;
1219        }
1220        if ((my+n) > child) {
1221            /* this case:
1222             *                    |------ me ------|
1223             *     |------ child ------|
1224             *     |-- mScrollX --|
1225             */
1226            return child-my;
1227        }
1228        return n;
1229    }
1230}
1231