滚动型.scrollTo不工作?保存在旋转滚动型位置存在、位置、工作、scrollTo

2023-09-12 11:27:35 作者:花落若相惜

确定..我必须忽视的东西真正简单的在这里,但我想我试图做一些相当基本的..只要保留了滚动型的方向变化滚动条的位置...

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...

下面是$ C $下我的onSaveInstanceState和onRestoreInstanceState .. sView是容器的滚动型布局。在我的滚动型是一个有很多textviews的LinearLayout中。

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.

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

}

如果我设置了吐司sViewX和sViewY对还原值,这些值保持和正确的。

If I set a Toast with the values of sViewX and sViewY on the Restore, the values are kept and correct.

编辑:我只是试图做一个sView.scrollTo(0150);在我的onCreate ..只是,看看是否会在150像素打开活动下来,才没有。我想我的问题已经做了.scrollTo方法。

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.

推荐答案

我想它了。

由于我使用的setText到TextViews在我的onCreate,呼吁.scrollTo将无法正常工作。

Since I'm using setText to TextViews in my onCreate, calling .scrollTo won't work.

所以,现在我使用的是以下内容:

So now I'm using the following:

sView.post(new Runnable() {
    @Override
    public void run() {
        sView.scrollTo(sViewX, sViewY);
    } 
});