Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android animation stop works in recyclerview when view go off the screen

I set animaion like this:

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {     
            Animation anim = AnimationUtils.loadAnimation(context, R.anim.rotate);
            holder.windPropellers.setAnimation(anim);
            break;
}

When the view scrolled off the screen animation stops. And when you scroll back it's not animating at all.

like image 666
Евгений Речков Avatar asked Jan 21 '17 13:01

Евгений Речков


2 Answers

You need to set transient state in the view to prevent it from being recycled.

Your code would look like this:

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {     
        Animation anim = AnimationUtils.loadAnimation(context, R.anim.rotate);
        holder.windPropellers.setHasTransientState(true);
        anim.setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                holder.windPropellers.setHasTransientState(false);
            }
        });
        holder.windPropellers.setAnimation(anim);
        break;

}

like image 139
jonathanrz Avatar answered Oct 17 '22 23:10

jonathanrz


I used a ProgressBar with a custom image/drawable instead because I couldn't get other answers to work flawlessly.

Set setVisibility(View.GONE/VISIBLE) in onBindViewHolder as needed.

<ProgressBar
    android:id="@+id/pbUpload"
    android:layout_width="52dp"
    android:layout_height="52dp"
    android:layout_gravity="center"
    android:indeterminate="true"
    android:indeterminateDrawable="@drawable/loader_rotate"
    android:indeterminateDuration="1000"
    android:visibility="gone" />

and the Drawable loader_rotate.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <rotate
            android:fromDegrees="0"
            android:toDegrees="360"
            android:pivotX="50%"
            android:pivotY="50%"
            android:drawable="@drawable/loader"> <!-- a png image in drawable folders -->
        </rotate>
    </item>
</layer-list>
like image 29
Pierre Avatar answered Oct 17 '22 23:10

Pierre