问题描述
相同的问题ScrollView .scrollTo不工作?保存在旋转滚动型位置
我动态地的onCreate项目添加到滚动视图。我尝试以下方法添加的所有项目后:
I dynamically add items to scrollView in onCreate. I try the following after all items were added:
// no effect
ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);
// no effect
ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);
mainScroll.post(new Runnable() {
public void run(){
ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);
mainScroll.scrollTo(0, 0);
}
});
// works like a charm
ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);
mainScroll.postDelayed(new Runnable() {
public void run(){
ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);
mainScroll.scrollTo(0, 0);
}
}, 30);
我的结论是有一些事件如DOM就绪?是否有任何回调?
I conclude that there is some event like 'DOM-ready'? Are there any callbacks?
推荐答案
您不必延长滚动型和创建自己的每通过的的<一个href="http://stackoverflow.com/questions/9753603/using-smoothscrollby-method-in-android-scrollview-class?rq=1">this问题。
You don't need to extend the ScrollView and create your own as per the answer provided by David Daudelin for the this question.
获得 ViewTreeObserver
的滚动型
并添加 OnGlobalLayoutListener
到 ViewTreeObserver
。然后调用从<$ C $的 onGlobalLayout()
方法 ScrollView.scrollTo(X,Y)
方法C> OnGlobalLayoutListener 。 code:
Get the ViewTreeObserver
of the ScrollView
and add an OnGlobalLayoutListener
to the ViewTreeObserver
. Then call the ScrollView.scrollTo(x,y)
method from the onGlobalLayout()
method of the OnGlobalLayoutListener
. Code:
ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);
ViewTreeObserver vto = scrollView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
mainScroll.scrollTo(0, 0);
}
});
这篇关于滚动型scrollTo不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!