运行延迟

时间:2016-08-03 18:02:05

标签: java android android-layout android-view

我使用ExplandableLayout并需要使用addView()命令动态加载控件。

当我在expandableLayout中添加控件时,我需要扩展它。但是由于我添加它时控件并没有真正在视图上绘制,我需要自己测量它,调用view.measure方法。在我调用view.getMeasuredHeight()之后调用它之后,它会返回正确的值。但是,如果我在小延迟中运行view.getMeasuredHeight(),则会再次返回0这是错误的。因此,我的扩展布局没有扩大到适当的大小。

小例子:

    new Handler().postDelayed(() -> {
                this.dateTimePickerContainer.setExpanded(true);
                System.out.println(this.getMeasuredHeight()); // log 0
                this.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
                System.out.println(this.getMeasuredHeight()); // log 500+ which is correcr value
                new Handler().postDelayed(() -> {
                    System.out.println(this.getMeasuredHeight()); //log 0 again, why?
    }, 100);}, 
    5000);

我怎么能在那些第二次尝试之后找到实际发生的事情以及为什么它再次变为0?

我实际上浪费了相当多的时间,而且目前没有想法。因此,即使是任何调试技巧,也会很棒。也许有一种方法可以监控,以某种方式与视图交互并记录此交互?

0 个答案:

没有答案
相关问题