Veljko
Veljko

Reputation: 1903

Android ListView/Gallery layout animation

I'm working with gallery widget. I've attached layout animation that fades in element in gallery. It works well.

But when I change content of gallery, and refresh adapter with notifyDataSetChanged(); New elements show up, but there is no layout animation for them.

How to implement that effect? I frequently update gallery content, and I need that effect every time.

Here is a code:

 <com.rtrk.gui.mainmenu.gallery.A4TVGallery
    android:id="@+id/mainMenuGallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:layoutAnimation="@anim/a4tv_gallery_controller"
    android:persistentDrawingCache="animation" />


// Notify new data
mainMenuAdapter.notifyDataSetChanged();

// Set default selection

mainMenuGallery.setSelection(defaultSelectedIndex);

TNX!

Upvotes: 0

Views: 1203

Answers (1)

Martin Rajniak
Martin Rajniak

Reputation: 640

Layout animation as it is described in documentation "Defines the layout animation to use the first time the ViewGroup is laid out." So this is used only the first time you show your view.

I have recently done something similar, and I don't know if it is the best solution, but you can start the animation in the adapter when the system calls the getView method. The important thing there is not to create an animation object in the getView method but rather in adapter constructor just once and then reuse the same animation for the views you would like to show with the desired animation.

If you would like to show the animation only on the new views than create a flag that will signal whether the view has been shown for the first time and use the view IDs to associate the view with the correct flag.

Use the setAnimation method on the view rather then startAnimation if you want to control when the animation will be fired. It is useful if you want to create an effect where the views are gradually appearing on the screen.

Upvotes: 2

Related Questions