private void ItemsSourceCollectionChanged(NotifyCollectionChangedEventArgs e)
{
// Update the cache
if(e.Action == NotifyCollectionChangedAction.Remove && e.OldItems != null)
{
for(int index = 0; index < e.OldItems.Count; index++)
{
_items.RemoveAt(e.OldStartingIndex);
}
}
if(e.Action == NotifyCollectionChangedAction.Add && e.NewItems != null && _items.Count >= e.NewStartingIndex)
{
for(int index = 0; index < e.NewItems.Count; index++)
{
_items.Insert(e.NewStartingIndex + index, e.NewItems[index]);
}
}
if(e.Action == NotifyCollectionChangedAction.Replace && e.NewItems != null && e.OldItems != null)
{
foreach(object t in e.NewItems)
{
_items[e.NewStartingIndex] = t;
}
}
// Update the view
if(e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Replace)
{
if(e.OldItems != null)
{
foreach(object t in e.OldItems)
{
_view.Remove(t);
}
}
}
if(e.Action == NotifyCollectionChangedAction.Reset)
{
// Significant changes to the underlying data.
ClearView();
if(ItemsSource != null)
{
_items = new List<object>(ItemsSource.Cast<object>().ToList());
}
}
// Refresh the observable collection used in the selection adapter.
RefreshView();
}