RecyclerView content height
Asked Answered
G

3

24

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.

Gyron answered 7/2, 2015 at 21:3 Comment(0)
R
20

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.

Ram answered 9/2, 2015 at 5:39 Comment(4)
Considering the nature of the RECYCLERView, won't that just get the child views that are actually created? Say I have a 100 items, maybe only 10 views are inflated and recycled.Gyron
If you are trying to get height of items that are not laid out, you cannot because they are not laid out. There cannot be a solution for this besides laying out every single item, which defeats the purpose of using RecyclerView.Ram
But the RecylerView knows enough to layout the scrollbars correctly, so it must know something about it's contents height?Gyron
It approximates the total size of all items by taking the average size of current views multiplied by the number of items in the adapter.Ram
T
52

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.

Toilet answered 26/7, 2016 at 17:51 Comment(1)
this method always return 0Rubbing
R
20

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.

Ram answered 9/2, 2015 at 5:39 Comment(4)
Considering the nature of the RECYCLERView, won't that just get the child views that are actually created? Say I have a 100 items, maybe only 10 views are inflated and recycled.Gyron
If you are trying to get height of items that are not laid out, you cannot because they are not laid out. There cannot be a solution for this besides laying out every single item, which defeats the purpose of using RecyclerView.Ram
But the RecylerView knows enough to layout the scrollbars correctly, so it must know something about it's contents height?Gyron
It approximates the total size of all items by taking the average size of current views multiplied by the number of items in the adapter.Ram
R
6

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.

Repertoire answered 5/5, 2020 at 17:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.