出现之前测量Recyclerview [英] Measure Recyclerview before it appears
问题描述
在测量recyclerView之前,我目前遇到问题。我需要measuredHeight才能启动扩展动画。
I am currently experiencing an issue when measuring a recyclerView before it appears. I need the measuredHeight in order to start an "expand" animation.
这是以前在我正在处理的代码中为gridView完成的,我正尝试迁移并使用GridLayoutManager将其保存到RecyclerView
This was previously done for a gridView in the code I'm working on and I am trying to migrate it to a RecyclerView with GridLayoutManager
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
final int heightSpec = View.MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, View.MeasureSpec.AT_MOST);
mGridView.measure(widthSpec, heightSpec);
int targetHeight = mGridView.getMeasuredHeight();
它正在使用gridView,但是如果我在recyclerView上调用具有相同度量规格的度量方法,则结果始终是16777215,我认为这可能是某物的最大值,但是我不能说什么。
It was working with the gridView but if I call measure method with same measure specs on the recyclerView, result is always 16777215 which I think might be a max value for something but I cannot say what.
我看到一些帖子解释说,视图可以在呈现到屏幕上之前进行测量通过使用以下度量规格对其进行测量来显示屏幕:
I saw some post explaining that a view can be measured before it is rendered on screen by measuring it with following measure specs :
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
final int heightSpec = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
但是我获得0的recyclerView.getMeasuredHeight();。
but I get 0 for recyclerView.getMeasuredHeight();.
有没有一种方法可以在将recyclerView呈现在屏幕上之前正确测量其高度?
Is there a way to properly measure the height of the recyclerView before it is rendered on screen ?
谢谢。
推荐答案
是否要在添加视图之前对其进行测量?如果是这样,那就很危险。
Are you trying to measure the view before adding it? If so, that is dangerous.
此外,就RecyclerView而言,现有的布局管理器还不支持WRAP_CONTENT。它们依赖于支持match_paren&的基本实现。确切的尺寸。
Also, in terms of RecyclerView, unfortunately, existing layout managers don't yet support WRAP_CONTENT. They rely on the base implementation which supports match_paren & exact dimensions.
如果您知道商品的高度,则可以扩展GridLayoutManager,覆盖onMeasure并在那里进行测量。
If you know your item's height, you can extend GridLayoutManager, override onMeasure and measure it there yourself.
这篇关于出现之前测量Recyclerview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!