I have a rather complex ListView, with variable list item heights. Under certain conditions, I need to display an additional view in a list item, which is hidden by default (View.GONE
). By enabling it (View.VISIBLE
), the list item grows in height (or at least it's supposed to).
The problem: Even though I declare the item's root layout to wrap_content
, and each component in the item to fill_parent
, the view I hide/show which is supposed to change the item's height is simply cut off at the bottom instead of its parent (the item layout) growing in height to fully display it.
Are there any gotchas related to ListViews and item layouts and item height which I may have missed?
Some more observations:
For testing purposes, I have now reduced the list item layout to just contain the root LinearLayout and an ImageView. When I set the LinearLayout height to e.g. 200dip
and the ImageView to fill_parent
, I would have expected the ImageView to grow until it hits the 200dip
limit set by its parent.
However, the image will instead be only ever as tall as its bitmap resource (as if I had set it to wrap_content
) and the whole list item will be of the same height (i.e. as if I had set it to wrap_content, too).
If however, I set the image height to e.g. 200dip
, then the list item will grow in height, and so will the item layout.
In other words, the layout_height
of the list item layout is completely ignored, and so is any height value on ImageView other than a hard-coded pixel value.
Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.
A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.
I managed to fix this, but I don't understand why.
As I mentioned, I had set the layout_height
of the list item layout to wrap_content
(since fill_parent
is meaningless here, considering that a ListView is indefinitely tall).
However, I had set the layout_height
of all views inside that layout to fill_parent
. The problem disappeared when setting them to wrap_content
instead.
This raises two other questions:
1) What are the semantics of a view asking to fill_parent
, when the parent wraps_content
? Which size request takes precedence?
2) How would I ever make a view fill a list item if fill_parent
apparently doesn't work?
Thanks for your input guys.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With