How can I animate newly added items in ListView
?
I have a adapter
and when I add new items in my list I say adapter.notifyDataSetChanged();
the items are added, everything works perfectly, but my problem is I want newly added element to have some animation.
Adding this kind of animations is harder than I first thought of. There are two ways depending of the kind of animation you are trying to achieve.
Using LayoutAnimationController. There is an example in the API demos.
Animating each
View
:This is quite a hack but the only way I found to add an animation to
ListView
's children is the following:You can try notifying the adapter the id of the item you are willing to delete and call
adapter.notifyDataSetChanged();
. This will generate calls to the adapter'sgetView()
method. Inside it you can do something like:After the animation finished you can recall
adapter.notifyDataSetChanged()
to put everything in place.Animate
each added element in thegetView()
method of yourCustom Adapter
.And thereby achieve the
Animation
.The official docs about animation in Android say that you can set an animation to trigger whenever the layout is changed, using android:animateLayoutChanges="true".
Taken from: http://developer.android.com/training/animation/layout.html