android - Xamarin Listview grouping -
i dealing listview in xamarin.froms. can populate listview listitem each record:
[ {"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"} ]
//9 items in listview json source
but want group items on key value, "cat"
here , achieve this:
any suggestion or approach toward appreciated.
you need enable grouping this:
mylistview.isgroupingenabled = true; mylistview.groupdisplaybinding = new binding("groupkey"); // see below
and add data in groups (e.g. lists of lists). means need create own class show groupings, such as:
public class grouping<k, t> : observablecollection<t> { // nb: groupdisplaybinding above displaying 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); } }
and finally, add data in groups:
var groups = new observablecollection<grouping<string, mydataclass>>(); // can pass set of data in (where "groupa" enumerable set) groups.add(new grouping<string, mydataclass>("groupa", groupa)); // or filter down set of data groups.add(new grouping<string, mydataclass>("groupb", myitems.where(a => a.somefilter()))); mylistview.itemsource = groups;
bind cell mydataclass have before:
var cell = new datatemplate(typeof(textcell)); cell.setbinding(textcell.textproperty, "somepropertyfrommydataclass"); cell.setbinding(textcell.detailproperty, "otherpropertyfrommydataclass"); mylistview.itemtemplate = cell;
credit link in @pnavk's answer.
check out explanation on why use template k instead of string in grouping
class, how customise header look, , more:
http://motzcod.es/post/94643411707/enhancing-xamarinforms-listview-with-grouping
Comments
Post a Comment