自定义视图内的尺寸变化的ViewGroup后未呈现 [英] Views inside a custom ViewGroup not rendering after a size change

查看:161
本文介绍了自定义视图内的尺寸变化的ViewGroup后未呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我快到,有我难住了一个问题,我希望有人可以给我一些指点。

I'm running into a problem that has me stumped and I was hoping someone could give me some pointers.

我正在使用自定义的ViewGroup(实际上是的FrameLayout包含RelativeLayout的)来呈现事件的日历应用程序。日历内的事件被重新psented作为次$ P $被根据事件的持续时间和含视图的大小尺寸。

I'm working on an application that uses a custom ViewGroup (actually a FrameLayout that contains a RelativeLayout) to render an event calendar. The events inside the calendar are represented as views that are sized according to the duration of the event and the size of the containing view.

我快到时,含有​​的FrameLayout调整大小时出现的问题。当前实现消除所有这一切重新present的事件,并企图增加新的并且基于的FrameLayout的电流大小来计算其大小的意见。这项工作是通过观的onSizeChanged()方法,该方法是在的FrameLayout覆盖被触发。

I'm running into a problem that occurs when the containing FrameLayout is resized. The current implementation removes all of the views that represent the events and attempts to add new ones and calculate their sizes based on the current size of the FrameLayout. This work is being triggered via the onSizeChanged() method of View which is overridden in the FrameLayout.

在视图大小,这code执行和视图更新,但是,他们没有实际呈现在屏幕上......包含的FrameLayout内的意见,纯粹是不可见的。如果我加载在hierarchyviewer工具的视图,它们是视图树的一部分,而在它们应该在的位置的概览中进行了概述,但它们不显示。 (请注意,意见是在初始呈现的FrameLayout可见...它只有在它们消失一个调整大小。)

When the view is resized, this code is executed and the views are updated, however, none of them actually render on the screen... the views contained within the FrameLayout are simply not visible. If I load the view in the hierarchyviewer tool, they are part of the view tree, and are outlined in the overview in the positions they should be in, but they do not display. (Note that the views are visible on the initial render of the FrameLayout... it's only after a resize that they disappear.)

似乎事件的大小调整期间顺序如下:

It appears that the order of events during the resize is as follows:

onMeasure()
onMeasure()
onSizeChanged()
onLayout()

复位意见后调用requestLayout()(内部onSizeChanged())似乎没有任何效果。不过,如果我造成了一些延误调用requestLayout()之前,将意见变得可见。我可能会导致这种延迟通过生成一个线程,睡觉,或创建一个虚拟按钮,简单地调用requestLayout()和$ P $后调整pssing它自己,甚至这丑陋的黑客放置在onSizeChanged()的末尾:

Calling requestLayout() after resetting the views (inside onSizeChanged()) seems to have no effect. However, if I cause some delay before calling requestLayout(), the views become visible. I can cause this delay by spawning a thread and sleeping, or by creating a dummy button that simply calls requestLayout() and pressing it after the resize myself, or even this ugly hack placed at the end of onSizeChanged():

post(new Runnable() {
    public void run() {
        requestLayout();
    }
});

当我使用这个技巧,所包含的观点是可见的,事件的顺序如下:

When I use this hack, the contained views are visible, and the order of events is as follows:

onMeasure()
onMeasure()
onSizeChanged()
onLayout()
onMeasure()
onMeasure()
onLayout()

这样看来,迫使第二招通(视图树后已被修改),使所包含的观点可见,因为他们应该。为什么延迟调用requestLayout()这是否是一个谜给我。

So it appears that forcing a second measure pass (after the view tree has been modified) makes the contained views visible as they should be. Why delaying the call to requestLayout() does this is a mystery to me.

任何人都可以提供任何指针,以什么我做错了什么?

Can anyone provide any pointers as to what I'm doing wrong?

我知道这是有些困难,而不看一些code可循,所以我创建了具有我的问题一个小样本应用程序,并使其在Github可用:

I realize that this is somewhat difficult to follow without looking at some code, so I have created a small sample application that exhibits my issue and made it available on Github:

<一个href="https://github.com/MichaelSims/ViewGrou$p$psizeTest">https://github.com/MichaelSims/ViewGrou$p$psizeTest

我上面提到的黑客致力于一个单独的分支:

The hack I mentioned above is committed to a separate branch:

<一个href="https://github.com/MichaelSims/ViewGrou$p$psizeTest/tree/post-runnable-hack">https://github.com/MichaelSims/ViewGrou$p$psizeTest/tree/post-runnable-hack

如果我可以提供任何其他信息,请让我知道,在此先感谢。

If I can provide any additional information, please let me know and thanks in advance.

推荐答案

这不是黑客攻击,它是如何它应该工作。 onSizeChanged()被调用作为布局传递的一部分,你不能/不应该requestLayout()在一个布局传递。这是正确的张贴requestLayout()在事件队列表明您已经在布局通改变View层次结构。

It's not a hack, it's how it's supposed to work. onSizeChanged() is invoked as part of the layout pass, and you cannot/should not requestLayout() during a layout pass. It is correct to post a requestLayout() on the event queue to indicate that you have changed the View hierarchy during a layout pass.

这篇关于自定义视图内的尺寸变化的ViewGroup后未呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆