forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactiveListAdapter.cs
More file actions
75 lines (61 loc) · 2.09 KB
/
ReactiveListAdapter.cs
File metadata and controls
75 lines (61 loc) · 2.09 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
using System;
using System.Collections.Generic;
using System.Reactive.Disposables;
using System.Threading;
using Android.Views;
using Android.Widget;
using Splat;
namespace ReactiveUI
{
public class ReactiveListAdapter<TViewModel> : BaseAdapter<TViewModel>, IEnableLogger
where TViewModel : class
{
readonly IReadOnlyReactiveList<TViewModel> list;
readonly Func<TViewModel, ViewGroup, View> viewCreator;
readonly Action<TViewModel, View> viewInitializer;
IDisposable _inner;
public ReactiveListAdapter(
IReadOnlyReactiveList<TViewModel> backingList,
Func<TViewModel, ViewGroup, View> viewCreator,
Action<TViewModel, View> viewInitializer = null)
{
this.list = backingList;
this.viewCreator = viewCreator;
this.viewInitializer = viewInitializer;
_inner = this.list.Changed.Subscribe(_ => NotifyDataSetChanged());
}
public override TViewModel this[int index] {
get { return list[index]; }
}
public override long GetItemId(int position) {
return list[position].GetHashCode();
}
public override bool HasStableIds {
get { return true; }
}
public override int Count {
get { return list.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View theView = convertView;
var data = list[position];
if (theView == null) {
theView = viewCreator(data, parent);
}
var ivf = theView.GetViewHost() as IViewFor<TViewModel>;
if (ivf != null) {
ivf.ViewModel = data;
}
if (viewInitializer != null) {
viewInitializer(data, theView);
}
return theView;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
Interlocked.Exchange(ref _inner, Disposable.Empty).Dispose();
}
}
}