Sweet animation

Mikael Magnusson
I am a developer
Published in
1 min readNov 1, 2015

For a recent app I was developing I needed to clear all the items in a ListView. And instead of just wiping the adapter and presenting the user with an empty list I wanted to give the impression of the list actually getting rid of the list items, one by one. Just like the animation in Jelly Bean when you click to clear all notifications.

As for many things I turned to StackOverflow for help, and below is the final implementation.

The methods getFirstVisiblePosition and getLastVisiblePosition do exactly what they say. But a gotcha: they return the position not the index.

Also, the getChildAt method (which takes an index not a position) will always return visible children. This detail had me scratching my head.

The magic that makes this code work is the setStartOffset. It let’s each view start its own animation with a delay (offset), which is key in an effect like the one we’re trying to achieve.

And after all is said and done, we wipe the adapter.

(Originally published 2013–02–21)

--

--