在android中如何在方向改变时保持滚动状态

在android中如何在方向改变时保持滚动状态,android,user-interface,android-widget,Android,User Interface,Android Widget,我创建了一个包含horizontalscrollview的自定义视图。现在,当我更改方向时,它的滚动状态每次都会更改为0 我使用onSaveInstanceState(Bundle savedInstanceState)和onRestoreInstanceState(Bundle savedInstanceState)获得了上一个滚动状态。在onrestoreinnstancestate中,我使用以下方法重新定位滚动 hoz_scroll.scrollTo(hoz_x_pos, hoz_scro

我创建了一个包含horizontalscrollview的自定义视图。现在,当我更改方向时,它的滚动状态每次都会更改为0

我使用
onSaveInstanceState(Bundle savedInstanceState)
onRestoreInstanceState(Bundle savedInstanceState)
获得了上一个滚动状态。在
onrestoreinnstancestate
中,我使用以下方法重新定位滚动

hoz_scroll.scrollTo(hoz_x_pos, hoz_scroll.getScrollY());
但根本没有效果。
谢谢你的帮助。谢谢。

我想你需要设置

android:configChanges="orientation"
AndroidManifest.xml
中的活动元素中


然后,您需要在活动中覆盖配置更改(配置配置)中的public void onconfiguration changed。

我认为您需要设置

android:configChanges="orientation"
AndroidManifest.xml
中的活动元素中


然后,您需要在活动中重写配置更改(配置配置)。

如果必须在每次方向更改位置重置时调用onCreate。 您可以通过将方向更改添加到清单来避免它,但不确定当前滚动状态是否完整。 我几天前研究过它。如果你的界面在每个方向上都有stadar维度,那么你可以通过采样许多滚动值来找到一个等式

使用getScrollY()创建一个地图,该地图在横向和横向上显示相同的文本。价值越高越好。然后使用matlab或papper使用多项式插值创建多项式方程。 更多值->更精确

还必须像这样滚动到某个位置

hoz_scroll.post(new Runnable() {

    public void run() {
        scroller.scrollTo(hoz_x_pos,hoz_scroll.getScrollY());
    }
});

如果必须在每次方向更改时调用onCreate,则位置将重置。 您可以通过将方向更改添加到清单来避免它,但不确定当前滚动状态是否完整。 我几天前研究过它。如果你的界面在每个方向上都有stadar维度,那么你可以通过采样许多滚动值来找到一个等式

使用getScrollY()创建一个地图,该地图在横向和横向上显示相同的文本。价值越高越好。然后使用matlab或papper使用多项式插值创建多项式方程。 更多值->更精确

还必须像这样滚动到某个位置

hoz_scroll.post(new Runnable() {

    public void run() {
        scroller.scrollTo(hoz_x_pos,hoz_scroll.getScrollY());
    }
});

将此代码放在活动中,以便在方向更改时不会滚动。你甚至不需要编码

@Override
public void onConfigurationChanged(Configuration config){
    super.onConfigurationChanged(config);
}

将此代码放在活动中,以便在方向更改时不会滚动。你甚至不需要编码

@Override
public void onConfigurationChanged(Configuration config){
    super.onConfigurationChanged(config);
}

以下是一个可行的解决方案:

private ScrollView scroll;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_content);


    TextView title=(TextView) findViewById(R.id.textView1);
    TextView content=(TextView) findViewById(R.id.textView2);

    title.setText(getIntent().getStringExtra("title"));
    content.setText(getIntent().getStringExtra("content"));

    scroll = (ScrollView) findViewById(R.id.scrollView1);
    if(savedInstanceState!=null){
        final int y=savedInstanceState.getInt("position");
        scroll.post(new Runnable() {

            public void run() {
                scroll.scrollTo(0,y);
            }
        });
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("position", scroll.getScrollY());
}

以下是一个可行的解决方案:

private ScrollView scroll;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_content);


    TextView title=(TextView) findViewById(R.id.textView1);
    TextView content=(TextView) findViewById(R.id.textView2);

    title.setText(getIntent().getStringExtra("title"));
    content.setText(getIntent().getStringExtra("content"));

    scroll = (ScrollView) findViewById(R.id.scrollView1);
    if(savedInstanceState!=null){
        final int y=savedInstanceState.getInt("position");
        scroll.post(new Runnable() {

            public void run() {
                scroll.scrollTo(0,y);
            }
        });
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("position", scroll.getScrollY());
}

我设置了android:configChanges=“orientation”,但我不会进入onConfigurationChanged…请不要使用configChange黑客。一次也没有。这不是配置更改的目的。我设置了android:configChanges=“orientation”,但我不会进入onConfigurationChanged…请不要使用configChange黑客。一次也没有。这不是配置更改的目的+1谢谢!你的代码可以工作。但是为什么没有runnable它就不能工作呢?你知道吗?因为scrollview还没有分配到视图中。+1谢谢!你的代码可以工作。但是为什么没有runnable它就不能工作呢?因为scrollview还没有在视图中分配。