如何解决使用外部视图观察者的视图高度值
我在布局文件中的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 举报,一经查实,本站将立刻删除。