Modify the Recyclerview before it appears.

I am currently having a problem measuring recyclerView before it appears. I need a measured value to start the "expand" animation.

This was done earlier for the gridView in the code I'm working on, and I'm trying to port it to RecyclerView using the 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(); 

It worked with gridView, but if I call a measurement method with the same measurement parameters in recyclerView, the result is always 16777215, which I think may be the maximum value for something, but I can’t say that.

I saw a message explaining that a view can be measured before it is displayed on the screen by measuring it using the following measurement parameters:

 final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); final int heightSpec = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); 

but I get 0 for recyclerView.getMeasuredHeight () ;.

Is there a way to correctly measure the height of a recyclerView before it is displayed on the screen?

Thanks.

+3
android android-view android-recyclerview android-viewgroup
source share
2 answers

Are you trying to measure the view before adding it? If so, it is dangerous.

In addition, from the point of view of RecyclerView, unfortunately, existing layout managers do not yet support WRAP_CONTENT. They rely on a basic implementation that supports match_paren and accurate sizes.

If you know the height of your element, you can expand the GridLayoutManager, override onMeasure and measure it there yourself.

+1
source share

Thanks to the yigit post, I could finally calculate the future recyclerView height by manually inflating the child view, measuring it with

 final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); final int heightSpec = View.MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, View.MeasureSpec.AT_MOST); 

And finally, multiplying your measured weight by the number of rows contained in gridLayoutManager. Hope support WRAP_CONTENT will be available soon for RecycleViews layout managers.

+2
source share

All Articles