-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathSectionedAdapter.cs
More file actions
66 lines (47 loc) · 1.46 KB
/
Copy pathSectionedAdapter.cs
File metadata and controls
66 lines (47 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using Microsoft.Maui.Adapters;
namespace VirtualListViewSample;
public class SectionedAdapter : VirtualListViewAdapterBase<Section, Item>
{
public SectionedAdapter(IList<Section> items) : base()
{
Items = items;
}
public readonly IList<Section> Items;
public override Section GetSection(int sectionIndex)
=> Items[sectionIndex];
public override int GetNumberOfSections()
=> Items.Count;
public override int GetNumberOfItemsInSection(int sectionIndex)
=> Items[sectionIndex].Count;
public override Item GetItem(int sectionIndex, int itemIndex)
=> Items[sectionIndex][itemIndex];
public void AddItem(string sectionTitle, string itemName)
{
var section = Items.FirstOrDefault(s => s.Title == sectionTitle);
if (section is null)
{
section = new Section { Title = sectionTitle };
Items.Add(section);
}
section.Add(new Item { Name = itemName });
InvalidateData();
}
public void RemoveItem(int sectionIndex, int itemIndex)
{
var section = Items.ElementAtOrDefault(sectionIndex);
if (section is null)
return;
section.RemoveAt(itemIndex);
if (section.Count <= 0)
Items.RemoveAt(sectionIndex);
InvalidateData();
}
public void UpdateItem(int sectionIndex, int itemIndex)
{
var section = Items.ElementAtOrDefault(sectionIndex);
if (section is null)
return;
section[itemIndex].Name = $"{section[itemIndex].Name} Updated";
InvalidateItems(new ItemPosition(sectionIndex, itemIndex));
}
}