Why does running a second viewpropertyanimation on a view break the animation listeners?

ε祈祈猫儿з 提交于 2019-11-29 17:44:10

that happens because the call to animate() returns always the same ViewPropertyAnimator object. It's an internal object of that particular view.

I believe it is like that for efficiency reasons, no need to create new object on every call.

For us developers, that means that all parameters that we set on that object are kept between calls. So if you call setDuration(1234) and later on there's another call to animate, it will still use 1234ms as the duration. Same thing for delay, interpolator or setListener.

So, the way to make it work is to always reset any parameters that you're not using for that animation. Meaning you should call .setListener(null) to any animation that does not use listener.

You could as well create a helper method like:

static ViewPropertyAnimator animate(View view){
    return view.animate()
        .setListener(null)
        .setDuration(DEFAULT_DURATION)
        .setStartDelay(0)
        .setInterpolator(DEFAULT_INTERPOLATOR);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!