0

我正在开发一个应用程序,当用户更改屏幕方向以及用户退出应用程序时,该应用程序需要保存和恢复 textview 滚动位置,由于文本块非常长,我正在使用多个 textViews(8-10)。我从这里的一些答案中得到了帮助,但它们似乎不起作用,请建议一种更好的方法来在活动中包含非常大的文本块。代码如下:

提前致谢,

第1章.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>

第1章.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);
        }
    });
}
}
4

2 回答 2

0

您需要保存组件的状态才能在旋转后恢复它...

于 2014-07-30T13:27:36.723 回答
0

尝试这个 :

     //  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]);
        }
    });
  }
于 2014-07-30T13:24:49.690 回答