Android ViewFlipper 测量未显示的子项
我的主 Activity 视图上有一个 ViewFlipper 。在 onCreate 中,我实例化了添加到 ViewFlipper 的视图。之后我将显示的孩子设置为第一个。当单击按钮时,我将 ViewFlipper 的显示子视图切换到第二个视图。在调用 viewFlipper.setDisplayedChild(1) 之后,我需要进行一些计算并实例化一些基于显示子项的宽度和高度的对象。
这里的主要问题是,第一次(第二次 = 1)显示子项时,视图的测量尚未完成。所以我的宽度和高度是 0。如果我返回 (setDisplayedChild(0)
),然后返回到第二个孩子,那就可以了(宽度和高度是正确的)。
我对视图的行为做了一些研究,发现有一个方法 [onMeasure
](http://developer.android.com/reference/android/view/View.html#onMeasure (int, int)) 可以被覆盖,一切都可以在这里完成。好吧,应该如何。实际上是这样,但问题是 onMeasure
在每个视图切换上被调用 4 次。我的发现是,第一次和第三次调用的宽度和高度是全屏大小。第二次和第四次调用是正确的宽度和高度。因为此时我必须实例化新对象,所以在 onMeasure
中执行此操作是不明智的。
还有其他正确的方法吗? 是否可以强制测量? 我真的需要在屏幕上显示测量视图吗?
问候 兹梅达
I have a ViewFlipper on my main Activity's View. At onCreate I instante Views which are added to ViewFlipper. After that I set displayed child to first one. And when a button is clicked I switch ViewFlipper's display child to second view. Right after calling viewFlipper.setDisplayedChild(1)
I need to do some calculations and instantate some objects which are based on width and height of displayed child.
Main problem here is that at first time (second = 1) child is displayed measurement of view is not done yet. So my width and height are 0. If I go back (setDisplayedChild(0)
) and than goes back to second child it is ok (width and height are correct).
I have done some research about that behavior of views and found out that there is a method [onMeasure
](http://developer.android.com/reference/android/view/View.html#onMeasure(int, int)) which can be overrided and everything can be done here. OK that how it should be. Actually it is but problem is that onMeasure
is called 4 times on each view switch. And my findings was that at first and third call width and height are full screen sized. The second and fourth call are proper width and height. Because I have to instantate new objects at that point it is unwise to do it in onMeasure
.
Is there any other, proper way of doing it?
Is it possible to force measurement?
Do I really need to show view on screen for measurements?
Regards
Zmeda
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 init-action (基于显示的子项的宽度和高度)应该在测量 ViewFlipper 的子项后调用,您可以使用 post 方法来实现,例如:
Your init-action (based on width and height of displayed child) should be invoked after measuring of ViewFlipper's children, you can achieve that using method post, e.g.: