将滚动视图恢复到Onresume上的位置
问题描述:
滚动视图和滚动后的许多按钮,并在滚动视图从顶部开始返回到布局时选择按钮。我希望滚动视图从用户停止的地方开始。请帮助我在Android的初学者,所以请简单解释。将滚动视图恢复到Onresume上的位置
答
在活动或片段被毁坏之前,您必须保存您的滚动视图的位置。
您可以在onSaveInstanceState
//save value on onSaveInstanceState
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putIntArray("SCROLL_POSITION",
new int[]{ mScrollView.getScrollX(), mScrollView.getScrollY()});
}
保存值,然后将其还原上onRestoreInstanceState
//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]);
}
});
}
以上仅仅是一个例子详细内容见THIS BLOG和This post on SO。