Reputation: 4065
Is there any good way of getting the height of the content inside RecyclerView? Considering all items may have different height.
In my scenario I'm using a LinearLayoutManager.
I've searched around and found surprisingly few results for this problem.
Upvotes: 24
Views: 35983
Reputation: 7322
I use to experiment with View.measure method:
recycler.measure(View.MeasureSpec.makeMeasureSpec(recycler.width, View.MeasureSpec.EXACTLY), View.MeasureSpec.UNSPECIFIED)
val height = recycler.measuredHeight
If your items are simple enough (e.g. with fixed heights), it should work.
Upvotes: 6
Reputation: 633
RecyclerView.computeVerticalScrollRange
will give an estimation - it averages the height of the rows being currently displayed and multiplies that by the result of getItemCount
computeHorizontalScrollRange
for width if you're orientation is horizontal.
Upvotes: 52
Reputation: 38243
RecyclerView is a ViewGroup
. You can use getChildAt
/ getChildCount
to get views. Then from the view, you can call getWidth
/ getHeight
.
If you have ItemDecorators and need to get size including them, you can use LayoutManager
's getDecorated<xxx>
methods.
Upvotes: 20