Android ViewFlipper 测量未显示的子项

发布于 2024-11-07 13:54:06 字数 809 浏览 0 评论 0原文

我的主 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早茶月光 2024-11-14 13:54:06

您的 init-action (基于显示的子项的宽度和高度)应该在测量 ViewFlipper 的子项后调用,您可以使用 post 方法来实现,例如:

    viewFlipper.setDisplayedChild(1);

    viewFlipper.post(new Runnable() {
        @Override
        public void run() {
            Log.d("test", "w: " + viewFlipper.getCurrentView().getWidth());// not 0
            //do some calculations and instantate some objects
        }
    });

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.:

    viewFlipper.setDisplayedChild(1);

    viewFlipper.post(new Runnable() {
        @Override
        public void run() {
            Log.d("test", "w: " + viewFlipper.getCurrentView().getWidth());// not 0
            //do some calculations and instantate some objects
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文