Android ScrollView .scrollTo 不起作用?在旋转时保存 ScrollView 位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3263259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ScrollView .scrollTo not working? Saving ScrollView position on rotation
提问by kefs
Ok.. I must be overlooking something real simple here, but i think i'm trying to do something fairly basic.. Simply retain the scrollbar position of a ScrollView on orientation change...
好吧..我必须在这里忽略一些真正简单的东西,但我想我正在尝试做一些相当基本的事情..只需在方向更改时保留 ScrollView 的滚动条位置...
Here is the code for my onSaveInstanceState and onRestoreInstanceState.. sView is the container for the ScrollView layout. Within my scrollview is a linearlayout with a lot of textviews.
这是我的 onSaveInstanceState 和 onRestoreInstanceState 的代码。sView 是 ScrollView 布局的容器。在我的滚动视图中是一个带有大量文本视图的线性布局。
@Override
public void onSaveInstanceState(Bundle outState)
{
//---save whatever you need to persist—
outState.putInt("sViewX",sView.getScrollX());
outState.putInt("sViewY",sView.getScrollY());
super.onSaveInstanceState(outState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
sViewX = savedInstanceState.getInt("sViewX");
sViewY = savedInstanceState.getInt("sViewY");
sView.scrollTo(sViewX, sViewY);
}
If I set a Toast with the values of sViewX and sViewY on the Restore, the values are kept and correct.
如果我在还原时使用 sViewX 和 sViewY 的值设置 Toast,则这些值将保持正确。
Edit: I just tried to do a sView.scrollTo(0,150); in my onCreate.. just to see if that would open the activity at 150px down, and it didn't. I think my issue has to do with the .scrollTo method.
编辑:我只是试着做一个 sView.scrollTo(0,150); 在我的 onCreate 中 .. 只是为了看看这是否会以 150px 向下打开活动,但它没有。我认为我的问题与 .scrollTo 方法有关。
回答by kefs
I figured it out.
我想到了。
Since I'm using setText
to TextViews in my onCreate
, calling .scrollTo
won't work.
由于我在我的 中使用setText
TextViews onCreate
,.scrollTo
因此无法调用。
So now I'm using the following:
所以现在我正在使用以下内容:
sView.post(new Runnable() {
@Override
public void run() {
sView.scrollTo(sViewX, sViewY);
}
});
回答by Ruslan Kayumov
this code worked for me for dynamically scrolling tabs, maybe it will be useful for u
这段代码适用于我动态滚动标签,也许它对你有用
TabHost tabHost;
int currentActiveTab;
HorizontalScrollView tabsHorizontalScrollView;
//some code ...
tabHost = getTabHost();
tabsHorizontalScrollView = findViewById(R.id.tabsHorizontalScrollView);
currentActiveTab = 8;
//some code ...
tabHost.setCurrentTab(currentActiveTab);
tabHost.getTabWidget().getChildAt(currentActiveTab).post(new Runnable() {
@Override
public void run() {
tabsHorizontalScrollView.scrollTo(tabHost.getTabWidget().getChildAt(currentActiveTab).getLeft(), tabsHorizontalScrollView.getScrollY());
}
});
回答by akd005
onRestoreInstanceState() is just to early to scroll the view. That's why posting new Runnable helps, but not always. Sometimes one even have to use postDelayed() to let it work. For Fragment one can use onViewCreated() instead :
onRestoreInstanceState() 只是为了早日滚动视图。这就是为什么发布新的 Runnable 会有所帮助,但并非总是如此。有时甚至必须使用 postDelayed() 来让它工作。对于 Fragment 可以使用 onViewCreated() 代替:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
sViewX = savedInstanceState.getInt("sViewX");
sViewY = savedInstanceState.getInt("sViewY");
sView.scrollTo(sViewX, sViewY);
}
回答by Anisetti Nagendra
This is working for me
这对我有用
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(SystemGlobal.SCROLL_Y, mRelativeLayoutMain.getTop());
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
mRelativeLayoutMain.scrollTo(0, savedInstanceState.getInt(SystemGlobal.SCROLL_Y));
}
回答by ivan.panasiuk
You should start scroll before components are drawn, since scroll does not work untill components are not created:
您应该在绘制组件之前开始滚动,因为在未创建组件之前滚动不起作用:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
scrollView.scrollTo(....
回答by Kirill
Instead of sending scroll action to next run-loop, you can scroll your view in global layout callback:
您可以在全局布局回调中滚动视图,而不是将滚动操作发送到下一个运行循环:
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
sView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
sView.scrollTo(sViewX, sViewY);
}
}
);
}
回答by Rodolfo De Los Santos
For MVVMCross:
对于 MVVMCross:
protected override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState);
ScrollView sv = FindViewById<ScrollView>(Resource.Id.dispatchScrollView);
int posY = sv.ScrollY;
outState.PutInt("scrollY", posY);
}
protected override void OnRestoreInstanceState(Bundle savedInstanceState)
{
base.OnRestoreInstanceState(savedInstanceState);
ScrollView sv = FindViewById<ScrollView>(Resource.Id.dispatchScrollView);
int posY = savedInstanceState.GetInt("scrollY");
sv.Post(new Runnable(new Action(() => sv.ScrollTo(0, posY))));
}