0

I am creating a custom view for my Android app that should replace a huge Listview with custom cells. My cells used a RelativeLayout and layout_height="match_parent"

Now I wonder how the ListView knows how high each cell needs to be because it does obviously not fill the parent. So how does it determine how large the distance between the top and bottom elements of the RelativeLayout is.

Explaination:

enter image description here

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ben
  • 199
  • 1
  • 13

1 Answers1

0

If you are just wandering how does the listview know the cell's sizes, it is thanks to her onMeasure() method

As you can read:

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

In shorts, with this and other methods, the ListView can measure the view's sizes and it will resize the view itself based on those measurements.

I hope this helped, if I misunderstood, please let me know

Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
  • Yea I thought that it would use onMeasure() but how do you measure a RelativeLayout with things attatched to the bottom. So maybe the question should be how do i know how large the distance between the top and the bottom elements is. – Ben Nov 21 '17 at 16:47
  • @Ben you should be more specific on what you are trying to obtain, can you try to explain/draw it and implement the question? – Pier Giorgio Misley Nov 21 '17 at 16:51
  • Added a picture for explaination. Hope its clear now – Ben Nov 21 '17 at 17:03
  • @Ben the point is: what you need this space for and what you should display in it? Because if you just need an item in the topside and another item in the bottom side, you should just put one below the other and set height to wrap content. Is there a reason for using that alignment? – Pier Giorgio Misley Nov 21 '17 at 17:12
  • Might be an option but my app looks better if the items are far apart. So I've done it like this so the items can be far apart on big screens and have less distance between them on smaller screens – Ben Nov 21 '17 at 17:19
  • @Ben why not using margins? – Pier Giorgio Misley Nov 21 '17 at 18:07