Skip to content

Commit

Permalink
RecyclerView adapter fixes
Browse files Browse the repository at this point in the history
According to the MSDN documentation NewStartIndex and OldStartIndex
can be -1 for certain operations.  This is stupid.  When this happens
we need to behave as if a Reset occurred.

Fixes an issue with Replace where we need to use the old index/items.
Fixes an issue with Move where if one item was moved (the default with
ObservableCollection) OldItems and NewItems were equal so the new/old
positions were equivalent.

Fixes #310
  • Loading branch information
kjeremy committed Oct 27, 2016
1 parent 259e1b8 commit bc44f69
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions MvvmCross.Droid.Support.V7.RecyclerView/MvxRecyclerAdapter.cs
Expand Up @@ -211,21 +211,28 @@ public virtual void NotifyDataSetChanged(NotifyCollectionChangedEventArgs e)
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
if (e.NewStartingIndex == -1)
goto case NotifyCollectionChangedAction.Reset;
NotifyItemRangeInserted(e.NewStartingIndex, e.NewItems.Count);
break;
case NotifyCollectionChangedAction.Move:
if (e.OldStartingIndex == -1 || e.NewStartingIndex == -1)
goto case NotifyCollectionChangedAction.Reset;
for (int i = 0; i < e.NewItems.Count; i++)
{
var oldItem = e.OldItems[i];
var newItem = e.NewItems[i];

NotifyItemMoved(this.ItemsSource.GetPosition(oldItem), this.ItemsSource.GetPosition(newItem));
var oldPosition = e.OldStartingIndex + i;
var newPosition = e.NewStartingIndex + i;
NotifyItemMoved(oldPosition, newPosition);
}
break;
case NotifyCollectionChangedAction.Replace:
NotifyItemRangeChanged(e.NewStartingIndex, e.NewItems.Count);
if (e.OldStartingIndex == -1)
goto case NotifyCollectionChangedAction.Reset;
NotifyItemRangeChanged(e.OldStartingIndex, e.OldItems.Count);
break;
case NotifyCollectionChangedAction.Remove:
if (e.OldStartingIndex == -1)
goto case NotifyCollectionChangedAction.Reset;
NotifyItemRangeRemoved(e.OldStartingIndex, e.OldItems.Count);
break;
case NotifyCollectionChangedAction.Reset:
Expand Down

0 comments on commit bc44f69

Please sign in to comment.