如何在 MvvmCross RecyclerView 中分组?

How to group by in MvvmCross RecyclerView?

如何在MvvmCross RecyclerView中分组? MvvmCross.Droid.Support.V7.RecyclerView 6.1.2

 <MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView               
                android:id="@+id/recyclerView"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:MvxItemTemplate="@layout/appointment_feed_item" 
                app:MvxBind="ItemsSource Items" />

MvvmCorss includes a neat mechanism for this since version 4.1.5. It’s called TemplateSelector and defined in the interface IMvxTemplateSelector. The purpose of this interface is to map your item to a layout id based on a rule that you can define.

因此不完全是 "group by",但您可以为组 header 使用一种布局,为组中的每个项目使用另一种布局。

public class TypeTemplateSelector : IMvxTemplateSelector
{
    private readonly Dictionary<Type, int> _typeMapping;

    public TypeTemplateSelector()
    {
        _typeMapping = new Dictionary<Type, int>
        {
            {typeof(HeaderGroupViewModel), Resource.Layout.header_group},
            {typeof(GroupItemViewModel), Resource.Layout.group_item}
        };
    }

    public int GetItemViewType(object forItemObject)
    {
        return _typeMapping[forItemObject.GetType()];
    }

    public int GetItemLayoutId(int fromViewType)
    {
        return fromViewType;
    }
}

并且在 axml 中,您使用选择器的完全限定 class 名称后跟程序集名称来设置它:

<MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView               
            android:id="@+id/recyclerView"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:MvxItemTemplate="@layout/appointment_feed_item"
            app:MvxTemplateSelector="RecyclerViewDifferentTemplates.Droid.TypeTemplateSelector,RecyclerViewDifferentTemplates.Droid"
            app:MvxBind="ItemsSource Items" />

来源和更多信息:RecyclerView TemplateSelector in MvvmCross