简体   繁体   English

Android:如何强制ScrollView布局,然后滚动到一个点?

[英]Android: how to force ScrollView layout, and then scroll to a point?

I have a ScrollView which I'm adding a number of custom Views to. 我有一个ScrollView,要向其中添加许多自定义视图。 After adding the custom Views, I would like to be able to Scroll to a particular one. 添加自定义视图后,我希望能够滚动到特定视图。 But, immediately after adding the custom views, the ScrollView hasn't been resized, so I can not scroll. 但是,添加自定义视图后,ScrollView尚未立即调整大小,因此无法滚动。 Any idea how I can force this resize? 知道如何强制调整大小吗?

for(int i = 0, i < numberOfViews, i++)
{
  scrollView.addView(new MyView(...));
}

//tried this, doesn't seem to be helpful 
scrollView.requestLayout();

//at this point scrollView dimensions are 0,0,0,0 
scrollView.scrollTo(5, 200);

An elegant solution is to plan the scroll and do it on the next onLayout(). 一个不错的解决方案是计划滚动并在下一个onLayout()上进行滚动。 Example code here: 示例代码在这里:

https://stackoverflow.com/a/10209457/1310343 https://stackoverflow.com/a/10209457/1310343

I'm not completely sure what you're trying to do, but I wanted to scroll all the way down, and couldn't just do that: I had to do it like this: 我不确定您要做什么,但我想一直向下滚动,不能只这样做:我必须这样做:

((ScrollView) findViewById(R.id.yourId)).post(new Runnable() {
         public void run() {
             ((ScrollView) findViewById(R.id.yourId)).fullScroll(View.FOCUS_DOWN);
         }
 });

It will update later, so your view is made :) 它将稍后更新,因此您的视图是:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM