diff options
| author | Adam Powell <adamp@google.com> | 2010-02-08 14:30:45 -0800 |
|---|---|---|
| committer | Adam Powell <adamp@google.com> | 2010-02-12 10:26:25 -0800 |
| commit | 0b8bb4282a7d1afb24f8c4d5beb2ca4ecc731116 (patch) | |
| tree | a889cd6a046d196878e4b05718e6cbe7c002db05 /core/java/android/widget/ScrollView.java | |
| parent | 8bd3f749e8c5348d1109b8ab66e0054d1a63eb73 (diff) | |
Overscrolling modifications. Overscroll will not allow the user to
scroll content out of view. Scrolling will slow down halfway to the
barrier point. API added in View. AbsListView, ScrollView,
HorizontalScrollView all use this API. Overscrolling uses haptic
feedback. Added scroll barrier pattern to config.xml.
Diffstat (limited to 'core/java/android/widget/ScrollView.java')
| -rw-r--r-- | core/java/android/widget/ScrollView.java | 67 |
1 files changed, 59 insertions, 8 deletions
diff --git a/core/java/android/widget/ScrollView.java b/core/java/android/widget/ScrollView.java index 4a1d8712ca2c..2ee7ad5517f9 100644 --- a/core/java/android/widget/ScrollView.java +++ b/core/java/android/widget/ScrollView.java @@ -459,7 +459,8 @@ public class ScrollView extends FrameLayout { final int deltaY = (int) (mLastMotionY - y); mLastMotionY = y; - super.scrollTo(mScrollX, mScrollY + deltaY); + overscrollBy(0, deltaY, 0, mScrollY, 0, getScrollRange(), + 0, getOverscrollMax()); break; case MotionEvent.ACTION_UP: final VelocityTracker velocityTracker = mVelocityTracker; @@ -470,8 +471,7 @@ public class ScrollView extends FrameLayout { if ((Math.abs(initialVelocity) > mMinimumVelocity)) { fling(-initialVelocity); } else { - final int bottom = Math.max(0, getChildAt(0).getHeight() - - (getHeight() - mPaddingBottom - mPaddingTop)); + final int bottom = getScrollRange(); if (mScroller.springback(mScrollX, mScrollY, 0, 0, 0, bottom)) { invalidate(); } @@ -485,6 +485,41 @@ public class ScrollView extends FrameLayout { } return true; } + + @Override + protected void onOverscrolled(int scrollX, int scrollY, + boolean clampedX, boolean clampedY) { + // Treat animating scrolls differently; see #computeScroll() for why. + if (!mScroller.isFinished()) { + mScrollX = scrollX; + mScrollY = scrollY; + if (clampedY) { + mScroller.springback(mScrollX, mScrollY, 0, 0, 0, getScrollRange()); + } + } else { + super.scrollTo(scrollX, scrollY); + } + } + + private int getOverscrollMax() { + int childCount = getChildCount(); + int containerOverscroll = (getHeight() - mPaddingBottom - mPaddingTop) / 3; + if (childCount > 0) { + return Math.min(containerOverscroll, getChildAt(0).getHeight() / 3); + } else { + return containerOverscroll; + } + } + + private int getScrollRange() { + int scrollRange = 0; + if (getChildCount() > 0) { + View child = getChildAt(0); + scrollRange = Math.max(0, + child.getHeight() - getHeight() - mPaddingBottom - mPaddingTop); + } + return scrollRange; + } /** * <p> @@ -858,9 +893,26 @@ public class ScrollView extends FrameLayout { @Override protected int computeVerticalScrollRange() { int count = getChildCount(); - return count == 0 ? getHeight() : (getChildAt(0)).getBottom(); + if (count == 0) { + return getHeight(); + } + + int scrollRange = getChildAt(0).getBottom(); + int scrollY = mScrollY; + int overscrollBottom = scrollRange - getHeight() - mPaddingBottom - mPaddingTop; + if (scrollY < 0) { + scrollRange -= scrollY; + } else if (scrollY > overscrollBottom) { + scrollRange += scrollY - overscrollBottom; + } + + return scrollRange; } + @Override + protected int computeVerticalScrollOffset() { + return Math.max(0, super.computeVerticalScrollOffset()); + } @Override protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) { @@ -915,10 +967,9 @@ public class ScrollView extends FrameLayout { int x = mScroller.getCurrX(); int y = mScroller.getCurrY(); - mScrollX = x; - mScrollY = y; - - if (oldX != mScrollX || oldY != mScrollY) { + if (oldX != x || oldY != y) { + overscrollBy(x - oldX, y - oldY, oldX, oldY, 0, getScrollRange(), + 0, getOverscrollMax()); onScrollChanged(mScrollX, mScrollY, oldX, oldY); } |
