Repository navigation
Expand file tree
/
Copy pathResultListAdapter.cs
More file actions
263 lines (226 loc) · 8.39 KB
/
Copy pathResultListAdapter.cs
File metadata and controls
263 lines (226 loc) · 8.39 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using Android.OS;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
namespace BmwDeepObd
{
public class ResultListAdapter : BaseAdapter<TableResultItem>
{
public delegate void CheckChangedEventHandler(TableResultItem resultItem);
public event CheckChangedEventHandler CheckChanged;
private readonly List<TableResultItem> _items;
public List<TableResultItem> Items => _items;
private readonly Android.App.Activity _context;
private readonly float _textWeight;
private readonly int? _textResId;
private readonly bool _showCheckBox;
private bool _ignoreCheckEvent;
private readonly Android.Content.Res.ColorStateList _defaultTextColors;
public ResultListAdapter(Android.App.Activity context, float textWeight, int? textResId, bool showCheckBox)
{
_context = context;
_items = new List<TableResultItem> ();
_textWeight = textWeight;
_textResId = textResId;
_showCheckBox = showCheckBox;
TextView dummy = new TextView(context);
_defaultTextColors = dummy.TextColors;
}
public ResultListAdapter(Android.App.Activity context, float textWeight, int textResId)
: this(context, textWeight, textResId, false)
{
}
public ResultListAdapter(Android.App.Activity context, float textWeight)
: this(context, textWeight, 0, false)
{
}
public ResultListAdapter(Android.App.Activity context)
: this(context, -1, 0, false)
{
}
public override long GetItemId(int position)
{
return position;
}
public override TableResultItem this[int position] => _items[position];
public override int Count => _items.Count;
public override bool IsEnabled(int position)
{
return false;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
TableResultItem item = _items[position];
View view = convertView ?? _context.LayoutInflater.Inflate(Resource.Layout.result_list, null);
CheckBox checkBoxSelect = view.FindViewById<CheckBox>(Resource.Id.checkBoxResultSelect);
if (_showCheckBox)
{
checkBoxSelect.Visibility = item.CheckVisible ? ViewStates.Visible : ViewStates.Invisible;
}
else
{
checkBoxSelect.Visibility = ViewStates.Gone;
}
_ignoreCheckEvent = true;
checkBoxSelect.Checked = item.Selected;
checkBoxSelect.Enabled = item.CheckEnable;
_ignoreCheckEvent = false;
checkBoxSelect.Tag = new TagInfo(item);
checkBoxSelect.CheckedChange -= OnCheckChanged;
checkBoxSelect.CheckedChange += OnCheckChanged;
View viewSeparator = view.FindViewById<View>(Resource.Id.ViewSeparator);
viewSeparator.Visibility = item.SeparatorVisible ? ViewStates.Visible : ViewStates.Gone;
TextView textView1 = view.FindViewById<TextView>(Resource.Id.ListText1);
TextView textView2 = view.FindViewById<TextView>(Resource.Id.ListText2);
if (textView1 != null)
{
try
{
textView1.Text = item.Text1;
SetTextApperance(textView1, _textResId);
if (item.TextColor != null)
{
textView1.SetTextColor(item.TextColor.Value);
}
else
{
textView1.SetTextColor(_defaultTextColors);
}
}
catch (Exception)
{
// ignored
}
}
if (item.Text2 == null)
{
textView2.Visibility = ViewStates.Gone;
}
else
{
if (textView2 != null)
{
try
{
textView2.Visibility = ViewStates.Visible;
textView2.Text = item.Text2;
SetTextApperance(textView2, _textResId);
if (item.TextColor != null)
{
textView2.SetTextColor(item.TextColor.Value);
}
else
{
textView2.SetTextColor(_defaultTextColors);
}
if (_textWeight >= 0)
{
LinearLayout.LayoutParams layoutPar = (LinearLayout.LayoutParams)textView2.LayoutParameters;
layoutPar.Weight = _textWeight;
textView2.LayoutParameters = layoutPar;
}
}
catch (Exception)
{
// ignored
}
}
}
return view;
}
private void OnCheckChanged(object sender, CompoundButton.CheckedChangeEventArgs args)
{
if (!_ignoreCheckEvent)
{
CheckBox checkBox = sender as CheckBox;
if (checkBox?.Tag is TagInfo tagInfo)
{
if (tagInfo.Info.Selected != args.IsChecked)
{
tagInfo.Info.Selected = args.IsChecked;
CheckChanged?.Invoke(tagInfo.Info);
NotifyDataSetChanged();
}
}
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416: Validate platform compatibility")]
private void SetTextApperance(TextView textView, int? resId)
{
if (resId == null)
{
return;
}
if (Build.VERSION.SdkInt < BuildVersionCodes.M)
{
#pragma warning disable 618
#pragma warning disable CA1422
textView.SetTextAppearance(_context, resId.Value);
#pragma warning restore CA1422
#pragma warning restore 618
}
else
{
textView.SetTextAppearance(resId.Value);
}
}
private class TagInfo : Java.Lang.Object
{
public TagInfo(TableResultItem info)
{
Info = info;
}
public TableResultItem Info { get; }
}
}
public class TableResultItem
{
private bool _selected;
public delegate void CheckChangeEventHandler(TableResultItem item);
public event CheckChangeEventHandler CheckChangeEvent;
public TableResultItem(string text1, string text2, object tag, bool checkVisible, bool selected, Android.Graphics.Color? textColor)
{
Text1 = text1;
Text2 = text2;
Tag = tag;
CheckVisible = checkVisible;
CheckEnable = true;
SeparatorVisible = true;
_selected = selected;
TextColor = textColor;
}
public TableResultItem(string text1, string text2, object tag, bool checkVisible, bool selected)
: this(text1, text2, tag, checkVisible, selected, null)
{
}
public TableResultItem(string text1, string text2, object tag)
: this(text1, text2, tag, false, false, null)
{
}
public TableResultItem(string text1, string text2)
: this(text1, text2, null, false, false, null)
{
}
public string Text1 { get; }
public string Text2 { get; }
public object Tag { get; }
public bool CheckVisible { get; }
public bool CheckEnable { get; set; }
public bool SeparatorVisible { get; set; }
public bool Selected
{
get { return _selected; }
set
{
bool changed = _selected != value && CheckChangeEvent != null;
_selected = value;
if (changed)
{
CheckChangeEvent(this);
}
}
}
public Android.Graphics.Color? TextColor { get; }
}
}