Forked from lbugnion/MVVM Light ObservableAdapter V0.0.1
Last active
August 29, 2015 14:01
-
-
Save Krumelur/844ccb23b3cad7f95597 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
namespace GalaSoft.MvvmLight.Helpers | |
{ | |
/// <summary> | |
/// A <see cref="BaseAdapter{T}"/> that can be used with an Android ListView. After setting | |
/// the <see cref="DataSource"/> and the <see cref="GetTemplate"/> properties, the adapter is | |
/// suitable for a list control. If the DataSource is an <see cref="INotifyCollectionChanged"/>, | |
/// changes to the collection will be observed and the UI will automatically be updated. | |
/// </summary> | |
/// <typeparam name="T">The type of the items contained in the <see cref="DataSource"/>.</typeparam> | |
////[ClassInfo(typeof(ObservableAdapter<T>), | |
//// VersionString = "0.0.1", | |
//// DateString = "201405262134", | |
//// UrlContacts = "http://www.galasoft.ch/contact_en.html", | |
//// Email = "[email protected]")] | |
public class ObservableAdapter<T> : BaseAdapter<T> | |
{ | |
private IList<T> _list; | |
private INotifyCollectionChanged _notifier; | |
/// <summary> | |
/// Gets the number of items in the DataSource. | |
/// </summary> | |
public override int Count | |
{ | |
get | |
{ | |
return _list == null ? 0 : _list.Count; | |
} | |
} | |
/// <summary> | |
/// Gets or sets the list containing the items to be represented in the list control. | |
/// </summary> | |
public IList<T> DataSource | |
{ | |
get | |
{ | |
return _list; | |
} | |
set | |
{ | |
if (_list == value) | |
{ | |
return; | |
} | |
if (_notifier != null) | |
{ | |
_notifier.CollectionChanged -= NotifierCollectionChanged; | |
} | |
_list = value; | |
_notifier = _list as INotifyCollectionChanged; | |
if (_notifier != null) | |
{ | |
_notifier.CollectionChanged += NotifierCollectionChanged; | |
} | |
} | |
} | |
/// <summary> | |
/// Gets and sets a method taking an item's position in the list, the item itself, | |
/// and a recycled Android View, and returning an adapted View for this item. Note that the recycled | |
/// view might be null, in which case a new View must be inflated by this method. | |
/// </summary> | |
public Func<int, T, View, View> GetTemplate | |
{ | |
get; | |
set; | |
} | |
/// <summary> | |
/// Gets and sets a method that returns a unique ID for an item. If NULL the item's position is used. | |
/// </summary> | |
public Func<T, long> GetId { | |
get; | |
set; | |
} | |
/// <summary> | |
/// Gets the item corresponding to the index in the DataSource. | |
/// </summary> | |
/// <param name="index">The index of the item that needs to be returned.</param> | |
/// <returns>The item corresponding to the index in the DataSource</returns> | |
public override T this[int index] | |
{ | |
get | |
{ | |
return _list == null ? default(T) : _list[index]; | |
} | |
} | |
/// <summary> | |
/// Returns a unique ID for the item corresponding to the <see cref="position"/> parameter. | |
/// In this implementation, the method always returns the position itself. | |
/// </summary> | |
/// <param name="position">The position of the item for which the ID needs to be returned.</param> | |
/// <returns>A unique ID for the item corresponding to the <see cref="position"/> parameter.</returns> | |
public override long GetItemId(int position) | |
{ | |
if (this.GetId == null) | |
{ | |
return position; | |
} | |
return this.GetId (this [position]); | |
} | |
/// <summary> | |
/// Prepares the view (template) for the item corresponding to the <see cref="position"/> | |
/// in the DataSource. This method calls the <see cref="GetTemplate"/> method so that the caller | |
/// can create (if necessary) and adapt the template for the corresponding item. | |
/// </summary> | |
/// <param name="position">The position of the item in the DataSource.</param> | |
/// <param name="convertView">A recycled view. If this parameter is null, | |
/// a new view must be inflated.</param> | |
/// <param name="parent">The view's parent.</param> | |
/// <returns>A view adapted for the item at the corresponding position.</returns> | |
public override View GetView(int position, View convertView, ViewGroup parent) | |
{ | |
if (GetTemplate == null) | |
{ | |
return convertView; | |
} | |
var item = _list[position]; | |
var view = GetTemplate(position, item, convertView); | |
return view; | |
} | |
private void NotifierCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) | |
{ | |
NotifyDataSetChanged(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment