forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataSourceGrouping.cs
108 lines (97 loc) · 3.46 KB
/
DataSourceGrouping.cs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#region Copyright Syncfusion Inc. 2001-2022.
// Copyright Syncfusion Inc. 2001-2022. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// [email protected]. Any infringement will be prosecuted under
// applicable laws.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Syncfusion.DataSource;
using Android.Widget;
using Android.Views;
using Android.Graphics;
namespace SampleBrowser
{
public class DataSourceGrouping : SamplePage, IDisposable
{
#region Fields
private DataSource dataSource;
private ContatsViewModel viewModel;
private ListView listView;
private SearchView filterText;
#endregion
#region Override
public override Android.Views.View GetSampleContent(Android.Content.Context context)
{
LinearLayout linear = new LinearLayout(context) { Orientation = Orientation.Vertical };
listView = new ListView(context);
viewModel = new ContatsViewModel();
dataSource = new DataSource();
dataSource.Source = viewModel.ContactsList;
dataSource.LiveDataUpdateMode = LiveDataUpdateMode.AllowDataShaping;
listView.Adapter = new ContactsAdapter(dataSource, context);
dataSource.SortDescriptors.Add(new SortDescriptor("ContactName"));
dataSource.GroupDescriptors.Add(new GroupDescriptor()
{
PropertyName = "ContactName",
KeySelector = (object obj1) =>
{
var item = obj1 as Contacts;
return item.ContactName[0].ToString();
}
});
filterText = new SearchView(context);
filterText.SetIconifiedByDefault(false);
filterText.SetPadding(0, 0, 0, (int)(10 * context.Resources.DisplayMetrics.Density));
filterText.SetQueryHint("Search contact");
filterText.QueryTextChange += OnFilterTextChanged;
linear.AddView(new LinearLayout(context) { Focusable = true, FocusableInTouchMode = true }, 0, 0);
linear.AddView(filterText);
linear.AddView(listView);
return linear;
}
private void OnFilterTextChanged(object sender, SearchView.QueryTextChangeEventArgs e)
{
if (dataSource != null)
{
this.dataSource.Filter = FilterContacts;
this.dataSource.RefreshFilter();
}
}
private bool FilterContacts(object obj)
{
var contacts = obj as Contacts;
if (contacts.ContactName.ToLower().Contains(filterText.Query.ToLower())
|| contacts.ContactNumber.ToLower().Contains(filterText.Query.ToLower()))
{
return true;
}
else
{
return false;
}
}
#endregion
public void Dispose()
{
if (dataSource != null)
{
dataSource.Dispose();
dataSource = null;
}
if (listView != null)
{
listView.Dispose();
listView = null;
}
if (filterText != null)
{
filterText.Dispose();
filterText = null;
}
}
}
}