【发布时间】:2015-03-23 10:35:22
【问题描述】:
这是我的布局:
当方向改变时,我需要保存滚动位置。例如,如果屏幕在纵向模式下显示从中间名开始的布局,则在横向模式下应该从中间名开始。
【问题讨论】:
标签: android android-layout orientation
这是我的布局:
当方向改变时,我需要保存滚动位置。例如,如果屏幕在纵向模式下显示从中间名开始的布局,则在横向模式下应该从中间名开始。
【问题讨论】:
标签: android android-layout orientation
只需在滚动元素上设置 android:id。您的视图将自动保存其滚动位置。
来自 View.java:15554 的代码
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
Parcelable state = onSaveInstanceState();
if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
throw new IllegalStateException(
"Derived class did not call super.onSaveInstanceState()");
}
if (state != null) {
// Log.i("View", "Freezing #" + Integer.toHexString(mID)
// + ": " + state);
container.put(mID, state);
}
}
}
【讨论】:
NestedScrollView 中有一个RecyclerView 时,这对我不起作用,但下面 Umesh Clauhan 的解决方案有效。
要在手机方向改变时保存和恢复 ScrollView 的滚动位置,您可以执行以下操作: 在 onSaveInstanceState 方法中保存当前位置:
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putIntArray("ARTICLE_SCROLL_POSITION",
new int[]{ mScrollView.getScrollX(), mScrollView.getScrollY()});
}
然后在 onRestoreInstanceState 方法中恢复位置。请注意,我们需要将 Runnable 发布到 ScrollView 以使其工作:
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
final int[] position = savedInstanceState.getIntArray("ARTICLE_SCROLL_POSITION");
if(position != null)
mScrollView.post(new Runnable() {
public void run() {
mScrollView.scrollTo(position[0], position[1]);
}
});
}
在谷歌上找到了这个解决方案。归功于原始编码器。 :)
【讨论】:
mScrollView.scrollTo(position[0], position[1] + getScreenWidth()/4);
在我的三星平板电脑上,我不必添加太多代码(只需调用超类)。另外,我在 ListView 上已经有了名字。
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/search_text"
android:maxLines="1"
android:inputType="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter search string here" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doSearch"
android:clickable="true"
android:text="Search"
android:layout_toRightOf="@id/search_text"/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_of_books"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/search_text"
android:divider="@null"
android:orientation="vertical" />
</RelativeLayout>
【讨论】:
【讨论】:
在清单中执行此操作..
<activity
android:name=".YourActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/app_name" >
</activity>
【讨论】:
您如果您的活动在轮换中没有改变任何内容,您可以使用 android:configChanges="orientation|keyboardHidden|screenSize" 在此特定活动的清单文件中。但这显然是解决方法,而不是适当的解决方案。
注意:
如果您需要为横向和纵向使用不同的视图,或者如果您需要为不同的设备使用不同的视图,则应该使用 bundle 并在 onsaveinstancestate 中保存数据并在 oncreate 中检索数据。此方法停止重新创建活动,活动假定事情将由用户自己处理。
【讨论】: