Home » Android » android – layoutAnimation works only one time

android – layoutAnimation works only one time

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a LinearLayout with layoutAnimation property that is used for a customize ExpandableListView. like this :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layoutAnimation="@drawable/list_layout_controller"
    android:orientation="vertical" >

When I click on the list, the animation will be displayed. But it works only once and it will not animate the list in the next click. Why?

Please let me know, If you need more description.

Thank you

How to&Answers:

Layout Animations are run when views are added to the layout. So you will have to manually add/remove the views on every expand/collapse for the layout animation to work.

For this, you can maintain a Map of views with the list index as the key.
Every time you get click, just pick the corresponding items from the map and add to your layout. The animation will run on that view and your problem is fixed.

You can also try by changing the visibility of views in onClick(). But not sure about this.
Try and let me know.

Hope that helps.

Answer:

First u want to declare

Animation  listanimation = AnimationUtils.loadAnimation(mainActivity.this, R.anim.list_layout_controller);

and everytime clearAnimation first.
In clickevent of LinearLayout

 linearLayout.clearAnimation();
 linearLayout.startAnimation(listanimation);

Answer:

call

listView.scheduleLayoutAnimation();

before all changes in the list

Answer:

Try to restart animation on each item click:

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
      view.getAnimation().start();
      //your custom logic
}
});

Answer:

Just use

public void startLayoutAnimation ()

http://developer.android.com/reference/android/view/ViewGroup.html#startLayoutAnimation()