Search code examples
androidmvvmcross

How can I dynamically set the height of an MvxListView upon adding a list item?


I have an MvxListView that I'm populating by clicking an "Add" button, which functions fine. The problem is that the list view's height needs to expand when I add an item. I've tried setting android:layout_height="wrap_content", but that doesn't seem to work. Here's the code for the button and list:

<Button
        android:text="Add One"
        local:MvxBind="Click AddSomething" />
    <Mvx.MvxListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        local:MvxItemTemplate="@layout/listitem_template"
        local:MvxBind="ItemsSource ProjectModel.Something" />

Solution

  • I'm not sure what you're trying to do makes that much sense...

    If you want the list to grow beyond the current screen-height, then use a non-zero size initially - e.g. use android:layout_height="fill_parent" or android:layout_height="200dp" initially - the list will then grow and will eventually scroll as the list contents get long enough to cause overflow.

    If you really want a control that changes size as items are added, then you can use MvxLinearLayout instead - but beware that if the list gets large then:

    • this will not scroll (you can help it scroll using a ScrollView but then you have to provide that with a size initially too)
    • this LinearLayout will not be very efficient (it won't reuse cells in the same way as ListView does).

    If you really want a ListView that dynamically sizes, then you can probably do something like inheriting from MvxListView, and you can then intercept when child views get added to work out and set the new height using SetLayoutParams- but I'm not sure that this will be that great a UX.