-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathDataSourceSorting.cs
157 lines (137 loc) · 5.28 KB
/
DataSourceSorting.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Syncfusion.DataSource;
using Android.Graphics;
namespace SampleBrowser
{
public class DataSourceSorting : SamplePage, IDisposable
{
#region Fields
private DataSource dataSource;
private ContatsViewModel viewModel;
private ListView listView;
private Spinner groupDropdown;
private GridLayout gridlayout;
private TextView groupTextView;
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);
listView.FastScrollEnabled = true;
viewModel = new ContatsViewModel();
dataSource = new DataSource();
dataSource.Source = viewModel.ContactsList;
dataSource.LiveDataUpdateMode = LiveDataUpdateMode.AllowDataShaping;
dataSource.SortDescriptors.Add(new SortDescriptor("ContactName", Syncfusion.DataSource.ListSortDirection.Ascending));
listView.Adapter = new ContactsAdapter(dataSource, context);
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;
}
public override View GetPropertyWindowLayout(Context context)
{
gridlayout = new GridLayout(context);
gridlayout.SetPadding(20, 20, 20, 20);
gridlayout.RowCount = 2;
gridlayout.ColumnCount = 2;
groupTextView = new TextView(context);
groupTextView.Text = "Sort by";
groupDropdown = new Spinner(context, SpinnerMode.Dialog);
var groupAdapter = new ArrayAdapter(context, Android.Resource.Layout.SimpleSpinnerItem);
groupAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
groupAdapter.Add("Ascending");
groupAdapter.Add("Descending");
groupDropdown.Adapter = groupAdapter;
groupDropdown.ItemSelected += GroupDropdown_ItemSelected;
gridlayout.AddView(groupTextView, LinearLayout.LayoutParams.WrapContent, (int)(30 * context.Resources.DisplayMetrics.Density));
gridlayout.AddView(groupDropdown);
return gridlayout;
}
private void GroupDropdown_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
var spinner = sender as Spinner;
var direction = spinner.GetItemAtPosition(e.Position).ToString();
dataSource.BeginInit();
dataSource.SortDescriptors.Clear();
dataSource.SortDescriptors.Add(new SortDescriptor()
{
PropertyName = "ContactName",
Direction = direction == "Ascending" ? Syncfusion.DataSource.ListSortDirection.Ascending : Syncfusion.DataSource.ListSortDirection.Descending
});
dataSource.EndInit();
(this.listView.Adapter as ContactsAdapter).NotifyDataSetChanged();
}
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;
}
}
public void Dispose()
{
if (dataSource != null)
{
dataSource.Dispose();
dataSource = null;
}
if (gridlayout != null)
{
gridlayout.Dispose();
gridlayout = null;
}
if (groupDropdown != null)
{
groupDropdown.Dispose();
groupDropdown = null;
}
if (groupTextView != null)
{
groupTextView.Dispose();
groupTextView = null;
}
if (listView != null)
{
listView.Dispose();
listView = null;
}
if (filterText != null)
{
filterText.Dispose();
filterText = null;
}
}
#endregion
}
}