Why do ListView items not grow to wrap their content?

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 eg 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 (ie as if I had set it to wrap_content, too).

If however I set the image height to eg 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.


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.


试试这个:http://www.java2s.com/Code/Android/UI/setListViewHeightBasedOnChildren.htm

public class Utils {

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); 
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }     
}

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() ):

v = getLayoutInflater().inflate(R.layout.list_item, parent, false);
链接地址: http://www.djcxy.com/p/93236.html

上一篇: 麻烦填充

下一篇: 为什么ListView项目不能增长以包装其内容?