滚动型scrollTo不起作用

编程入门 行业动态 更新时间:2024-10-28 12:18:57
本文介绍了滚动型scrollTo不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

相同的问题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?

推荐答案

您不必延长滚动型和创建自己的每通过的大卫Daudelin 的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不起作用

本文发布于:2023-11-03 01:03:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1553860.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   scrollTo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!