views:

1993

answers:

4

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.

+3  A: 

How are you inflating your rows?

If you are not using it right now, try using LayoutInflater#inflate(layoutId, parent, false) (where parent is the AdapterView supplied to getView() or newView()).

Note that I need to update my own book examples to reflect this -- Romain Guy pointed this out on a mailing list a month or two ago.

CommonsWare
Hi Mark, what are the semantics of supplying a root view to inflate(), but set attachToRoot to false? If the inlated view is not attached to rootView, what else is done with that view?
Matthias
I'm sorry, I forgot to mention: I pass null for rootView and false for attachToRoot. After all, the view is returned by adapter.getView(), which I expected takes care of adding it to the view tree.
Matthias
Quoting from the documentation, "If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML." Otherwise, Android does not know how to attach the child to the parent, and so I think it uses a very generic LayoutParams.
CommonsWare
thanks, I'll give that a shot
Matthias
+3  A: 

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.

Matthias
Thx for that Matthias =) I had the exakt same problem. I had also set the layout_height to "fill_parent". I thought that was obvious. But right you are, when setting to "wrap_content" it solved the issue.Weird...
Ted
A: 

HI.. Matthias

Please provide me an working example . am also facing the same problem , but am not clear how u solved. the problem.

A: 

I use the "AbsListView.LayoutParams" to configure the width and height manually inside "Adapter.getView()".

roys