Android:如何在改变屏幕方向/应用程序关闭后恢复textview滚动位置



我正在研究一个应用程序,需要保存和恢复textview滚动位置,当用户改变屏幕方向,当用户退出应用程序,我使用多个textViews(8-10)由于非常非常长的文本块。我从这里的一些答案的帮助,但他们似乎不工作,请建议一个更好的方式来包括非常非常大的文本块在一个活动。代码如下:

提前感谢,

chapter1.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollChapter1">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/Chapter1"
            android:textSize="17sp"
            android:padding="10px"
            android:id="@+id/txtChapter1Title"
            />
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dip"
            android:width="0dip"
            android:textSize="16sp"
            android:padding="10px"
            android:text="@string/Chapter1ContentA"
            android:id="@+id/txtChapter1A"
            android:maxLength="9000"
            android:layout_below="@+id/txtChapter1Title"
            />
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dip"
            android:width="0dip"
            android:textSize="16sp"
            android:padding="10px"
            android:text="@string/Chapter1ContentB"
            android:id="@+id/txtChapter1B"
            android:maxLength="9000"
            android:layout_below="@+id/txtChapter1A"
            />
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dip"
            android:width="0dip"
            android:textSize="16sp"
            android:padding="10px"
            android:text="@string/Chapter1ContentC"
            android:id="@+id/txtChapter1C"
            android:maxLength="9000"
            android:layout_below="@+id/txtChapter1B"
            />
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dip"
            android:width="0dip"
            android:textSize="16sp"
            android:padding="10px"
            android:text="@string/Chapter1ContentD"
            android:id="@+id/txtChapter1D"
            android:maxLength="9000"
            android:layout_below="@+id/txtChapter1C"
            />
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dip"
            android:width="0dip"
            android:textSize="16sp"
            android:padding="10px"
            android:text="@string/Chapter1ContentE"
            android:id="@+id/txtChapter1E"
            android:maxLength="9000"
            android:layout_below="@+id/txtChapter1D"
            />
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dip"
            android:width="0dip"
            android:textSize="16sp"
            android:padding="10px"
            android:text="@string/Chapter1ContentF"
            android:id="@+id/txtChapter1F"
            android:maxLength="9000"
            android:layout_below="@+id/txtChapter1E"
            />
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dip"
            android:width="0dip"
            android:textSize="16sp"
            android:padding="10px"
            android:text="@string/Chapter1ContentG"
            android:id="@+id/txtChapter1G"
            android:maxLength="9000"
            android:layout_below="@+id/txtChapter1F"
            />
</RelativeLayout>
</ScrollView>

chapter1.java:

public class chapter1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chapter1);
    WindowManager w = getWindowManager();
    Display d = w.getDefaultDisplay();
    if(d.getWidth() > d.getHeight()){
        Log.d("Orientation", "Landscape");
    }else{
        Log.d("Orientation", "Potrait");
    }
}
@Override
protected void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollChapter1);
    final RelativeLayout relativeLayout = (RelativeLayout) scrollView.getChildAt(0);
    final TextView textView = (TextView) relativeLayout.getChildAt(0);
    final int firstVisibleLineOffset = textView.getLayout().getLineForVertical(scrollView.getScrollY());
    final int firstVisibleCharacterOffset = textView.getLayout().getLineStart(firstVisibleLineOffset);
    outState.putInt("ScrollViewContainerTextViewFirstVisibleCharacterOffset", firstVisibleCharacterOffset);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
    super.onRestoreInstanceState(savedInstanceState);
    final int firstVisibleCharacterOffset = savedInstanceState.getInt("ScrollViewContainerTextViewFirstVisibleCharacterOffset");
    final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollChapter1);
    scrollView.post(new Runnable() {
        @Override
        public void run() {
            final RelativeLayout relativeLayout = (RelativeLayout) scrollView.getChildAt(0);
            final TextView textView = (TextView) relativeLayout.getChildAt(0);
            final int firstVisibleLineOffset = textView.getLayout().getLineStart(firstVisibleCharacterOffset);
            final int pixelOffset = textView.getLayout().getLineTop(firstVisibleLineOffset);
            scrollView.scrollTo(0, pixelOffset);
        }
    });
}
}

您需要保存组件的状态,以便在旋转后恢复它…

试试这个:

     //  two static variable,
public static int scrollX = 0;
public static int scrollY = -1;
  //update & save their value on onPause & onResume.
@Override
protected void onPause()
{
  super.onPause();
  scrollX = scrollView.getScrollX();
  scrollY = scrollView.getScrollY();
}

      @Override
  protected void onResume() {
    Util.Log("X:" + scrollX + " Y:" + scrollY);       
    super.onResume();
 //this is important. scrollTo doesn't work in main thread.
  scrollView.post(new Runnable(){
  @Override
 public void run()
 {
   scrollView.scrollTo(scrollX, scrollY);
 }
 });
 }

屏幕旋转:

       //save value on onSaveInstanceState
   protected void onSaveInstanceState(Bundle outState) {
   super.onSaveInstanceState(outState);
   outState.putIntArray("SCROLL_POSITION",
        new int[]{ mScrollView.getScrollX(), mScrollView.getScrollY()});
   }
      //Restore them on onRestoreInstanceState
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  final int[] position = savedInstanceState.getIntArray("SCROLL_POSITION");
  if(position != null)
    mScrollView.post(new Runnable() {
        public void run() {
            mScrollView.scrollTo(position[0], position[1]);
        }
    });
  }

最新更新