MvvmCross : dynamic item template selection for MvxListView
Asked Answered
U

1

5

If I have a view with the following MvxListView definition:

<Mvx.MvxListView
    android:layout_marginTop="10px"
    android:textFilterEnabled="true"
    android:choiceMode="singleChoice"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="20dp"
    local:MvxBind="ItemsSource Data; ItemClick LaunchCapabilityViewCmd"
    local:MvxItemTemplate="@layout/itemtemplate1" />

Instead of hard coding MvxItemTemplate to itemtemplate1, it it possible to dynamically set this based on the type of data I want to display in this view? I am looking for similar functionality to WPF's DateTemplateSelector.

TIA.

Urbanity answered 18/7, 2013 at 4:4 Comment(0)
N
7

You have to use a custom adapter to do this.

A few of the samples show how to use cell type selection. See:

e.g. from PolymorphicListItemTypesView.cs

        protected override View GetBindableView(View convertView, object source, int templateId)
        {
            if (source is Kitten)
                templateId = Resource.Layout.ListItem_Kitten;
            else if (source is Dog)
                templateId = Resource.Layout.ListItem_Dog;

            return base.GetBindableView(convertView, source, templateId);
        }

For Android, there is also an optimisation which should be added to the existing polymorphic adapter samples - to include use of GetItemViewType for better convertView reuse - see https://github.com/slodge/MvvmCross/issues/333

This questions is linked to:

Nickelous answered 18/7, 2013 at 6:54 Comment(1)
I've checked the sample, and it contains the optimizations. So you can close the issue :)Coparcenary

© 2022 - 2024 — McMap. All rights reserved.