package com.stanfy.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;

/**
 * Scroll view that can save its position.
 * @author Roman Mazur (Stanfy - http://www.stanfy.com)
 */
public class ScrollView extends android.widget.ScrollView {

  /** Froze scroll position flag. */
  private boolean frozeScrollPosition;

  public ScrollView(final Context context) {
    this(context, null);
  }

  public ScrollView(final Context context, final AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
  }

  public ScrollView(final Context context, final AttributeSet attrs, final int defStyle) {
    super(context, attrs, defStyle);
    init(context, attrs);
  }

  private void init(final Context context, final AttributeSet attrs) {
    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ScrollView);
    final boolean frozeSPos = a.getBoolean(R.styleable.ScrollView_frozeScrollPosition, true);
    a.recycle();
    setFrozeScrollPosition(frozeSPos);
  }

  /** @param frozeScrollPosition the frozeScrollPosition to set */
  public void setFrozeScrollPosition(final boolean frozeScrollPosition) { this.frozeScrollPosition = frozeScrollPosition; }

  @Override
  protected Parcelable onSaveInstanceState() {
    if (!frozeScrollPosition) { return super.onSaveInstanceState(); }
    final Parcelable parent = super.onSaveInstanceState();
    return new SavedState(parent, getScrollX(), getScrollY());
  }

  @Override
  protected void onRestoreInstanceState(final Parcelable state) {
    if (!frozeScrollPosition) { super.onRestoreInstanceState(state); }
    final SavedState myState = (SavedState)state;
    super.onRestoreInstanceState(myState.getSuperState());
    post(new Runnable() {
      @Override
      public void run() { scrollTo(myState.positionX, myState.positionY); }
    });
  }

  /**
   * Saved state for our scroll view.
   * @author Roman Mazur (Stanfy - http://www.stanfy.com)
   */
  static class SavedState extends BaseSavedState {

    /** Creator. */
    public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
      @Override
      public SavedState createFromParcel(final Parcel source) { return new SavedState(source); }
      @Override
      public SavedState[] newArray(final int size) { return new SavedState[size]; }
    };

    /** Saved values. */
    final int positionX, positionY;

    SavedState(final Parcelable parent, final int positionX, final int positionY) {
      super(parent);
      this.positionX = positionX;
      this.positionY = positionY;
    }

    private SavedState(final Parcel in) {
      super(in);
      positionX = in.readInt();
      positionY = in.readInt();
    }

    @Override
    public void writeToParcel(final Parcel dest, final int flags) {
      super.writeToParcel(dest, flags);
      dest.writeInt(positionX);
      dest.writeInt(positionY);
    }

  }

}

@@ -180,16 +180,42 @@
 buteSet;
+%0Aimport android.view.View;
 %0A%0A/**%0A *
@@ -1544,16 +1544,23 @@
 parent, 
+(float)
 getScrol
@@ -1567,20 +1567,68 @@
 lX()
-, getScrollY
+ / getContentWidth(), (float)getScrollY() / getContentHeight
 ());
@@ -1958,16 +1958,24 @@
  run() %7B
+%0A       
  scrollT
@@ -1976,16 +1976,22 @@
 crollTo(
+(int)(
 myState.
@@ -2003,37 +2003,314 @@
 ionX
-, myState.positionY); %7D%0A    %7D
+ * getContentWidth()), (int)(myState.positionY * getContentHeight()));%0A      %7D%0A    %7D);%0A  %7D%0A%0A  public int getContentWidth() %7B return getWidth(); %7D%0A  public int getContentHeight() %7B%0A    if (getChildCount() == 0) %7B return 0; %7D%0A    final View child = getChildAt(0);%0A    return child.getBottom() - child.getTop(
 );%0A 
@@ -2827,26 +2827,28 @@
 /%0A    final 
-in
+floa
 t positionX,
@@ -2898,34 +2898,36 @@
 e parent, final 
-in
+floa
 t positionX, fin
@@ -2929,18 +2929,20 @@
 , final 
-in
+floa
 t positi
@@ -3120,34 +3120,36 @@
 itionX = in.read
-In
+Floa
 t();%0A      posit
@@ -3162,18 +3162,20 @@
  in.read
-In
+Floa
 t();%0A   
@@ -3308,34 +3308,36 @@
       dest.write
-In
+Floa
 t(positionX);%0A  
@@ -3350,18 +3350,20 @@
 st.write
-In
+Floa
 t(positi

enhance scrollview