Created
February 6, 2015 13:26
-
-
Save runceel/801a3a5c5e80a1088305 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Android.App; | |
using Android.Content; | |
using Android.Runtime; | |
using Android.Views; | |
using Android.Widget; | |
using Android.OS; | |
using Codeplex.Reactive.Binding; | |
using Codeplex.Reactive; | |
using Codeplex.Reactive.Extensions; | |
namespace App1 | |
{ | |
[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")] | |
public class MainActivity : Activity | |
{ | |
private MainActivityViewModel viewModel = new MainActivityViewModel(); | |
protected override void OnCreate(Bundle bundle) | |
{ | |
base.OnCreate(bundle); | |
// Set our view from the "main" layout resource | |
SetContentView(Resource.Layout.Main); | |
this.FindViewById<Button>(Resource.Id.button1).Click += viewModel.AddItemCommand.ToEventHandler(); | |
var listView = this.FindViewById<ListView>(Resource.Id.listView1); | |
listView.Adapter = new RRCollAdapter<string>( | |
viewModel.Items, | |
() => LayoutInflater.FromContext(this).Inflate(Resource.Layout.layout1, null), | |
(x, v) => v.FindViewById<TextView>(Resource.Id.textView1).Text = x); | |
this.viewModel.Items.CollectionChangedAsObservable().Subscribe(_ => listView.InvalidateViews()); | |
} | |
} | |
class RRCollAdapter<T> : BaseAdapter<T> | |
{ | |
private ReadOnlyReactiveCollection<T> source; | |
private Func<View> createRowView; | |
private Action<T, View> setRowData; | |
public RRCollAdapter( | |
ReadOnlyReactiveCollection<T> source, | |
Func<View> createRowView, | |
Action<T, View> setRowData) | |
{ | |
this.source = source; | |
this.createRowView = createRowView; | |
this.setRowData = setRowData; | |
} | |
public override T this[int position] | |
{ | |
get { return source[position]; } | |
} | |
public override int Count | |
{ | |
get { return this.source.Count; } | |
} | |
public override long GetItemId(int position) | |
{ | |
return position; | |
} | |
public override View GetView(int position, View convertView, ViewGroup parent) | |
{ | |
if (convertView == null) | |
{ | |
convertView = this.createRowView(); | |
} | |
this.setRowData(this[position], convertView); | |
return convertView; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Codeplex.Reactive; | |
using System; | |
using System.Collections.ObjectModel; | |
namespace App1 | |
{ | |
public class MainActivityViewModel | |
{ | |
private ObservableCollection<string> source = new ObservableCollection<string> { "a", "b", "c" }; | |
public ReadOnlyReactiveCollection<string> Items { get; private set; } | |
public ReactiveCommand AddItemCommand { get; private set; } | |
public MainActivityViewModel() | |
{ | |
this.Items = source.ToReadOnlyReactiveCollection(); | |
this.AddItemCommand = new ReactiveCommand(); | |
this.AddItemCommand.Subscribe(_ => | |
{ | |
this.source.Add("item" + DateTime.Now); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment