2014-07-20 25 views
0

我正在处理一个应用程序,当用户改变屏幕方向和用户退出应用程序时,需要保存和恢复textview滚动位置,由于非常长的文本块,我使用了多个textViews(8-10) 。我在这里接受了一些答案的帮助,但他们似乎并不奏效,并且建议在活动中包含非常大的文本块的更好方法。下面的代码给出:提前Android:如何在更改屏幕方向/应用关闭后恢复textview滚动位置?

感谢,

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

回答

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

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

相关问题