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

时间:2014-07-20 11:03:34

标签: java android xml textview scroll-position

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

提前致谢,

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);
        }
    });
}
}

2 个答案:

答案 0 :(得分: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]);
        }
    });
  }

答案 1 :(得分:0)

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