Kenneth
Kenneth

Reputation: 4065

RecyclerView content height

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

Answers (3)

Berťák
Berťák

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

mdd-sbo
mdd-sbo

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

yigit
yigit

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

Related Questions