Xamarin Listview分组

Afz*_*Ali 7 android listview cross-platform xamarin xamarin.forms

我正在处理xamarin.froms中的列表视图。我可以轻松为每个记录填充一个listite的listview:

[
    {"cat":1, "name":"alpha"},
    {"cat":1, "name":"beta"},
    {"cat":1, "name":"gamma"},
    {"cat":2, "name":"john"},
    {"cat":2, "name":"william"},
    {"cat":2, "name":"smith"},
    {"cat":2, "name":"steve"},
    {"cat":3, "name":"abc"},
    {"cat":3, "name":"xyz"}
]
Run Code Online (Sandbox Code Playgroud)

// 9个列表源中的json源

在此处输入图片说明

但是我想要的是将所有项归为某个关键值,"cat"在这里说,实现以下目标:

具有分组的ListView

对此的任何建议或方法将不胜感激。

noe*_*cus 5

您需要启用以下分组:

    myListView.IsGroupingEnabled = true;
    myListView.GroupDisplayBinding = new Binding("GroupKey"); // See below
Run Code Online (Sandbox Code Playgroud)

然后将数据分组添加(例如列表列表)。这通常意味着您需要创建自己的类来显示分组,例如:

    public class Grouping<K, T> : ObservableCollection<T>
    {
        // NB: This is the GroupDisplayBinding above for displaying the header
        public K GroupKey { get; private set; } 

        public Grouping(K key, IEnumerable<T> items)ac
        {
            GroupKey = key;
            foreach (var item in items)
                this.Items.Add(item);
        }
    }
Run Code Online (Sandbox Code Playgroud)

最后,将数据分组添加:

    var groups = new ObservableCollection<Grouping<string, MyDataClass>>();

    // You can just pass a set of data in (where "GroupA" is an enumerable set)
    groups.Add(new Grouping<string, MyDataClass>("GroupA", GroupA)); 

    // Or filter down a set of data
    groups.Add(new Grouping<string, MyDataClass>("GroupB", 
        MyItems.Where(a => a.SomeFilter())));

    myListView.ItemSource = groups;
Run Code Online (Sandbox Code Playgroud)

像以前一样将单元格绑定到MyDataClass:

    var cell = new DataTemplate(typeof(TextCell));
    cell.SetBinding(TextCell.TextProperty, "SomePropertyFromMyDataClass");
    cell.SetBinding(TextCell.DetailProperty, "OtherPropertyFromMyDataClass");
    myListView.ItemTemplate = cell;
Run Code Online (Sandbox Code Playgroud)

归功于@pnavk答案中的链接。
查阅有关为什么在类中使用模板K而不是字符串的说明Grouping,如何自定义标题外观以及更多内容的说明:http :
//motzcod.es/post/94643411707/enhancing-xamarinforms-listview-with-分组