MvvmCross : dynamic item template selection for MvxListView

android-layout, itemtemplate, itemtemplateselector, mvvmcross, xamarin

Solution

You have to use a custom adapter to do this.

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

polymorphic lists in https://github.com/slodge/MvvmCross-Tutorials/tree/master/Working%20With%20Collections

grouped lists in the session list views in https://github.com/slodge/MvvmCross-Tutorials/tree/master/Sample%20-%20CirriousConference

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:

- Several item templates in one listbox in mvvmcross Android

- getViewTypeCount and getItemViewType methods of ArrayAdapter

Problem

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.

Original source

Related problems