Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

ListView Wrap Content Height

$
0
0

I have a ListView inside of a ScrollView. It would be great if simply setting the ListView height to wrap_content, but that'd be too easy right?

I have been trying to use a function to set the height. The problem is that listItem.Measure(0,0) throws a null exception. I think it's because the view's not created for that row yet?

` public static void SetListViewHeightBasedOnChildren(ListView listView) {

    if (listView.Adapter == null) 
        return;

    int totalHeight = 0;
    int count = listView.Adapter.Count;

    for (int i = 0; i < count; i++) {

        View listItem = listView.Adapter.GetView(i, null, listView);
        listItem.Measure(0,0);
        totalHeight += listItem.MeasuredHeight;

    }

    ViewGroup.LayoutParams p = new ViewGroup.LayoutParams(listView.LayoutParameters);
    p.Height = totalHeight + (listView.DividerHeight * (count -1));
    listView.LayoutParameters = p;
    listView.RequestLayout();

}

`

To officially know when the views have been laid out, I have to add logic to the adapter get view and note when each view is created ... What a pain in the butt!!!

What other options do I have?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>