[英]View height returns 0 when moving to another fragment and then navigate back android kotlin
I have two fragments
.我有两个
fragments
。 In fragment A
there is a RecyclerView
, when I click on the element of which I get to another fragment B
.在
fragment A
中有一个RecyclerView
,当我点击其中的元素时,我会到达另一个fragment B
。 In fragment A
I have a MaterialToolbar which can have different heights, so in onViewCreated()
I call materialToolbar.height
to get its height.在
fragment A
中,我有一个可以具有不同高度的 MaterialToolbar,因此在onViewCreated()
中,我调用materialToolbar.height
来获取它的高度。 When I open fragment A
everything works fine, however when I click on an element in the RecyclerView
, I get to fragment B
, in which on pressing the back button I will go back to the fragment A
and the height of my materialToolbar
will already be 0. Why does it return 0?当我打开
fragment A
时一切正常,但是当我单击RecyclerView
中的一个元素时,我进入fragment B
,在其中按下后退按钮我将返回到fragment A
并且我的materialToolbar
的高度已经是0. 为什么返回0? And how can I make it return the value I need?我怎样才能让它返回我需要的值?
There's an observer which listens to view-heirarchy of any given view.有一个观察者监听任何给定视图的视图层次结构。
toolbar.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
binding.mbSelect.viewTreeObserver.removeOnGlobalLayoutListener(this)
val height = materialToolbar.height
}
})
Try this above code in you fragment A, you will get the actual height toolbar在片段 A 中尝试上面的代码,您将获得实际高度的工具栏
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.