滚动型.scrollTo不工作?保存在旋转滚动型位置 [英] ScrollView .scrollTo not working? Saving ScrollView position on rotation

查看:114
本文介绍了滚动型.scrollTo不工作?保存在旋转滚动型位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于滚动型.scrollTo不工作?保存在旋转滚动型位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆