Android ScrollView scrollTo()方法不使用TextView?
例如,我的布局如下:Android ScrollView scrollTo()方法不使用TextView?,android,textview,scrollview,Android,Textview,Scrollview,例如,我的布局如下: <ScrollView> <LinearLayout> <LinearLayout> ... </LinearLayout> <LinearLayout> ... </LinearLayout> <LinearLayout> <TextView> </Text
<ScrollView>
<LinearLayout>
<LinearLayout>
...
</LinearLayout>
<LinearLayout>
...
</LinearLayout>
<LinearLayout>
<TextView>
</TextView>
</LinearLayout>
</LinearLayout>
</ScrollView>
scrollview.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
scrollview.scrollTo(x, y)
}
});
相反,它是有效的。为所有遇到相同问题的人发布答案。首先,如果所有线性布局都有相同的方向,为什么不只创建一次? 如果我没有错的话,ScrollView只能有一个子对象(一个直接子对象)。也许这就是你问题的原因。
但是,如果仍然需要这3个线性布局,可以创建一个FrameLayout作为父级来保存它们,并使该FrameLayout成为ScrollView的子级。看来我给Scroll打电话太早了。使用
scrview.post(new Runnable() {
public void run() {
scrview.scrollTo(0, 800);
}
});
相反,它是有效的。为任何遇到相同问题的人发布答案。您必须在runnable中调用
scrollTo
,因为scrollview尚未布局,所以在使用runnable时,您可以尝试屏幕闪烁。要解决此问题,您可以在scrollview上侦听lauyout更改,如下所示:
<ScrollView>
<LinearLayout>
<LinearLayout>
...
</LinearLayout>
<LinearLayout>
...
</LinearLayout>
<LinearLayout>
<TextView>
</TextView>
</LinearLayout>
</LinearLayout>
</ScrollView>
scrollview.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
scrollview.scrollTo(x, y)
}
});
我的错。我的问题不够清楚。这只是简单易懂的示例代码,所以我忘记了一些细节。我已经有一个父视图保存ScrollView中的所有视图。这个问题仍然存在。我编辑了我的问题800毫秒等待时间很长。“这真的有必要吗?有没有办法做到这一点而不选择一个可能太长或太短的任意时间延迟?”EdwinEvans answer在不需要任意延迟的情况下为我解决了一个相关问题。