Using GridView/ListView’s Built-in Animations

The GridView and ListView controls in Windows 8 both come with built-in animation for various operations. When the controls are bound to an ObservableCollection the will do a fancy animation whenever an item is

  • Added to the collection (even if it’s added in the middle of the collection, in which case the other elements will slide away to give room for the new element)
  • Removed from the collection

I mention the two animations/operations above specifically because I rarely see developers taking advantage of this built-in functionality. Most of the time, when applications fetch new data, they simply clear the previous data, and insert the new data, even though there might be elements that were both present in the new and the old data set. This is commonly seen in news/feed application that retrieve new posts — an obvious use for the built animations.

To see an example of these animations in action see my Open Nearby app (You might have to add a location in Denmark, if you are not from here). There is an option to filter the shops in the app bar. When this filter is applied the add/remove operations are used to filter away shops that no longer fit in the result set, and likewise it is filled up with new shops that does.

It can be a hassle to synchronize the old dataset for the GridView/ListView with freshly fetched data. Below is a few extension methods that will make it easier to get going:

public static void SyncCollection<T>(this ObservableCollection<T> observableCollection, IEnumerable<T> dataToSync, Func<T, T, bool> insertBefore, Func<T, T, bool> equality)
{
    var recentlyToRemove = observableCollection.Where(s => dataToSync.All(ss => !equality(ss,s))).ToList();
    var recentlyToAdd = dataToSync.Where(s => observableCollection.All(ss => !equality(ss, s)));
    foreach (var item in recentlyToRemove)
    {
        observableCollection.Remove(item);
    }
 
    foreach (var item in recentlyToAdd)
    {
        InsertInOrder(item, observableCollection, insertBefore);
    }
}
private static void InsertInOrder<T>(T item, ObservableCollection<T> observableCollection, Func<T, T, bool> insertBefore)
{
    for (int i = 0; i < observableCollection.Count; i++)
    {
        if (insertBefore(item, observableCollection[i]))
        {
            observableCollection.Insert(i, item);
            return;
        }
    }
    // if empty or last
    observableCollection.Add(item);
}

 

Here’s an example of usage from Open Nearby:

var newShops = ...// newly fetched shops from data service;

ObservableCollection<Shop> previousShops = ..// the old data current being shown in the view;

Qua.WS.ObservableCollectionExtensions.SyncCollection(previousShops, newShops,
(newShop, oldShop) => newShop.Distance.DistanceInKilometers < oldShop.Distance.DistanceInKilometers,
(shop1, shop2) => shop1.Id == shop2.Id);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s