diff options
| author | 2013-06-12 16:01:48 -0700 | |
|---|---|---|
| committer | 2013-06-12 17:42:14 -0700 | |
| commit | d6befbdd17f528d87e313b3665b248b670f8615c (patch) | |
| tree | fcf5550d8d96f03f707a89ce8d6a5049ad9293ec | |
| parent | 97f41383eb2bb098767ca153e470009fea810540 (diff) | |
Save Scroller position
- add SaveState with scroll position and use it during the first layout
Change-Id: I0806ddb9c52d70d32055f4b474d21f6e2e5a4ea8
| -rw-r--r-- | core/java/android/widget/ScrollView.java | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/core/java/android/widget/ScrollView.java b/core/java/android/widget/ScrollView.java index 0545d24c4dd5..b764c98843fb 100644 --- a/core/java/android/widget/ScrollView.java +++ b/core/java/android/widget/ScrollView.java @@ -16,6 +16,8 @@ package android.widget; +import android.os.Parcel; +import android.os.Parcelable; import com.android.internal.R; import android.content.Context; @@ -149,6 +151,8 @@ public class ScrollView extends FrameLayout { */ private static final int INVALID_POINTER = -1; + private SavedState mSavedState; + public ScrollView(Context context) { this(context, null); } @@ -1468,6 +1472,24 @@ public class ScrollView extends FrameLayout { } mChildToScrollTo = null; + // There is only one child + final View child = getChildAt(0); + final int childHeight = child.getMeasuredHeight(); + if (!hasLayout()) { + final int scrollRange = Math.max(0, + childHeight - (b - t - mPaddingBottom - mPaddingTop)); + if (mSavedState != null) { + mScrollY = mSavedState.scrollPosition; + mSavedState = null; + } // mScrollY default value is "0" + // Don't forget to clamp + if (mScrollY > scrollRange) { + mScrollY = scrollRange; + } else if (mScrollY < 0) { + mScrollY = 0; + } + } + // Calling this with the present values causes it to re-claim them scrollTo(mScrollX, mScrollY); } @@ -1637,4 +1659,58 @@ public class ScrollView extends FrameLayout { } return n; } + + @Override + protected void onRestoreInstanceState(Parcelable state) { + SavedState ss = (SavedState) state; + super.onRestoreInstanceState(ss.getSuperState()); + mSavedState = ss; + requestLayout(); + } + + @Override + protected Parcelable onSaveInstanceState() { + Parcelable superState = super.onSaveInstanceState(); + SavedState ss = new SavedState(superState); + ss.scrollPosition = mScrollY; + return ss; + } + + static class SavedState extends BaseSavedState { + public int scrollPosition; + + SavedState(Parcelable superState) { + super(superState); + } + + public SavedState(Parcel source) { + super(source); + scrollPosition = source.readInt(); + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + super.writeToParcel(dest, flags); + dest.writeInt(scrollPosition); + } + + @Override + public String toString() { + return "HorizontalScrollView.SavedState{" + + Integer.toHexString(System.identityHashCode(this)) + + " scrollPosition=" + scrollPosition + "}"; + } + + public static final Parcelable.Creator<SavedState> CREATOR + = new Parcelable.Creator<SavedState>() { + public SavedState createFromParcel(Parcel in) { + return new SavedState(in); + } + + public SavedState[] newArray(int size) { + return new SavedState[size]; + } + }; + } + } |