Search code examples
android-layoutxamarinmvvmcrossitemtemplateitemtemplateselector

MvvmCross : dynamic item template selection for MvxListView


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.


Solution

  • 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: