RecyclerView content height - android

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.

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.

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.

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.

Related

How to set a constraint layout width of an item in a recyclerview to a percentage of the parents width, not a fixed dp value?

I have a problem with recyclerview items width:
i already looked through android docs and stack so i think, there is no solution to this problem.
i have an ordinary recyclerview with a horizontal gridlayout
sorry, but i dont have enough reputation to embed pictures
Picture of recyclerview
and i want the items of it being shown next to each other, but I want to make the width of the items depend on the width of the parent (in this case, the recyclerview width used in the other fragment).
I can show you what i want by using a fixed value (in this example 200dp)
Picture of the solution I want, but here a fixed value for width is used
However if my constraint of the item is set to "match parent" like here:
Picture constraint set to match parent
The result is, that the width of the items seen in the phone always depends of the width of the recyclerview. in my case its cut nearly in half:
recyclerview cut in half
also android studio only allows one view as a top level layout, so i cannot use a guideline that is set to any %
possible solution to that?
If you have width of recycler view (in pixel not dp), just divide it by number of items in a row, and in your adapter in "onBindViewHolder" method set item width. this solution is when all of your item has same width.
In addition for height of every item use WRAP_CONTENT.

How to get height/width of a RecyclerView cell in onBindViewHolder?

I've seen other answers to this question but none worked for me; for example the top answer here (How to get the height of recyclerview item in "onBindViewHolder") says to do this:
view.measure(
View.MeasureSpec.makeMeasureSpec(recyclerViewWidth, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
but then doing view.getMeasuredHeight() only returns whatever I pass in for recyclerViewWidth.
Basically I want to set the size of components within the recycler view cell as fractions of the cell's size, but those fractions depend on the position of the cell. I'm a bit frustrated since this is so simple to do in iOS and seems so much more convoluted in android.
I am not a fan of setting percentages by code on Android. And if you do want to set your view's sizes relative to its parent, ContraintLayout are your best friend for setting items based on percentage guidelines, barriers, and ratios are something you should look into, it is efficient and you do not have to worry about calculation yourself.
https://developer.android.com/reference/androidx/constraintlayout/widget/ConstraintLayout

Modifying View width without calling requestLayout()

I have a FrameLayout with a 100 views (TextViews).
At some point I need to modify the width of 10 views.
Calling requestLayout() on 10 children as you know triggers a layout pass of all 100 views and it is very slow.
Is there a way to change only the width of these 10 views ?
I don't know if I explained well the problem. Basically what I need is to remeasure the View (TextView) so it will redraw itself using the new width value.

Get size of GridView when creating items within its adapter

I want to arrange a set of custom widgets inside a GridView. There are predefined templates which describe a count of elements and arrangement config. Max width and heigh for each of them have to be provided in order to avoid scrolling. How can I get a size of the displayed GridView when computing items size in adapter's "GetView" method?
You have access to few functions in order to get a GridView Height and Width as any other View. Look at the documentation here.
The size of a view is expressed with a width and a height. A view
actually possess two pairs of width and height values.
The first pair is known as measured width and measured height. These
dimensions define how big a view wants to be within its parent (see
Layout for more details.) The measured dimensions can be obtained by
calling getMeasuredWidth() and getMeasuredHeight().
The second pair is simply known as width and height, or sometimes
drawing width and drawing height. These dimensions define the actual
size of the view on screen, at drawing time and after layout. These
values may, but do not have to, be different from the measured width
and height. The width and height can be obtained by calling getWidth()
and getHeight().
Try doing a quick search on their doc next time, even if it's Xamarin, the Android doc is pretty reliable and you only need some adjustment to make it work.
(e.g.) : grivView.Width; instead of gridView.getWidth(); one being a property on Xamarin to follow C# standards, the other is a function.

How to determine an Android View's size in pixels?

Is there a direct programmatic way to get a fill_parent View's pixel height and width? For instance a view in a grid layout in a tab. Or do I have to get the window size and subtract the static sizes of the views around it?
You can get the views parent by calling myView.getParent() and with getWidth() and getHeight() you get the pixel you need.
the documentation is providing more information to this topic
http://developer.android.com/reference/android/view/View.html
There's the onMeasure() method, not sure if that will do what you need. http://developer.android.com/guide/topics/ui/custom-components.html

Categories

Resources