使用外部视图观察者的视图高度值

问题描述

我在布局文件中的image views中有3个(顶部,中部和底部constraint layout,并且每个高度的值如下:

constraint_layout.viewTreeObserver.addOnGlobalLayoutListener(object :
        ViewTreeObserver.OnGlobalLayoutListener {
        @SuppressLint("CheckResult")
        override fun onGlobalLayout() {

            top_height = top_image_view.height //height is ready
            middle_height = middle_image_view.height //height is ready
            bottom_height = bottom_image_view.height //height is ready

            constraint_layout.viewTreeObserver.removeOnGlobalLayoutListener(this)
            Timber.d("topHeight -> ${top_height }")
            Timber.d("middleHeight -> ${middle_height }")
            Timber.d("bottomHeight -> ${bottom_height }")
  }
})

// Do some calculations with the heights here

top_height,middle_height,and bottom_height全局变量。我想在观察者中使用这些值,因为我想基于更改视图的y位置来更改这些特定视图的可见性和位置,因此我无法在其中运行其中两个项目出于某种原因而使用同一线程。

但是当我从viewTreeObserver调用全局变量时,高度值再次变为0。我不确定该怎么做。

我们将不胜感激任何帮助或帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)