Trying to reset values from Property Animator to be used in recycler view
Asked Answered
O

1

0

Been doing some animation inside of a row in RecyclerView (not the row itself. Imagine expanding text) and there are times when the animation leaks to other recycled views which should not have that animation in them.

Since I use property animation the scale action resizes the inner view and the leak can be seen in 2 aspects: 1) The animation will go on (This I could overcome with some guards) 2) The view has been resized and stopped in its tracks and so it will be reflected in the recycled view.

How can I reset the views to their original state? I have tried many approaches from posts but none solved it. The closest definition I got was in this unanswered post: How to reset view to original state after using animators to animates its some properties?

Here is a sample of how I set up my animation in onBind (this one has an attempt to use onAnimationEnd which I found in one post but did not work)

ObjectAnimator scaleXUp = ObjectAnimator.ofFloat(mView, View.SCALE_X, 10f);
        scaleXUp.setRepeatCount(ValueAnimator.INFINITE);
        scaleXUp.setRepeatMode(ValueAnimator.REVERSE);
        scaleXUp.setDuration(700);
        ObjectAnimator scaleYUp = ObjectAnimator.ofFloat(mView, View.SCALE_Y, 10f);
        scaleYUp.setRepeatCount(ValueAnimator.INFINITE);
        scaleYUp.setRepeatMode(ValueAnimator.REVERSE);
        scaleYUp.setDuration(700);
        mTotalAnimation = new AnimatorSet();
        mTotalAnimation.play(scaleXUp).with(scaleYUp);
        mTotalAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        mTotalAnimation.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                animation.removeListener(this);
                animation.setDuration(0);
                for(Animator va : ((AnimatorSet)animation).getChildAnimations()) {
                    ((ValueAnimator)va).reverse();
                }
            }
        });
        mTotalAnimation.start();

And here is what I do in the onUnbindData:

if (mTotalAnimation != null) {
        mTotalAnimation.end();
        mTotalAnimation = null;
    }

And as I saw many people like the clearAnimation approach - tried and did not work either.

Occasional answered 11/8, 2017 at 7:26 Comment(0)
O
2

4 days and not one response but meanwhile I solved it myself. So the approach was close, just wrongly placed.

I added this method:

private void stopAnimation() {
    for (Animator anim : mTotalAnimation.getChildAnimations()) {
        ((ObjectAnimator) anim).reverse();
        anim.end();
    }
}

and I call that when I want to reset the view.

Here I am getting the animations from the AnimatorSet and reversing and ending each. I did not understand why I had to do it manually but it looks like this ability will be added in Android O: https://developer.android.com/preview/api-overview.html#aset

Starting in Android O, the AnimatorSet API now supports seeking and playing in reverse. Seeking lets you set the position of the animation set to a specific point in time. Playing in reverse is useful if your app includes animations for actions that can be undone. Instead of defining two separate animation sets, you can play the same one in reverse.

Occasional answered 15/8, 2017 at 19:58 Comment(1)
I don't think end() is necessary. It appears to undo the effect of reverse(). Also, to reset values to original state immediately, you will need anim.setDuration(0) before calling anim.reverse() Also I noticed that calling mTotalAnimation.setDuration(0) had no effect, it had to be called on each child animation.Transmittance

© 2022 - 2024 — McMap. All rights reserved.