If the user clicks on a CheckBox
, it runs a very nice animation (on Lollipop).
However if i invoke myCheckBox.toggle()
programatically, it does not run the animation, but immediately shows the new state. Is there a way to run the smooth animation when changing the checked state programatically?
EDIT: it turned out it does not run the animation when this is a RecyclerView
item. It runs otherwise...
EDIT2: the problem lies in the item animator. It seems it kills of all other animations before starting its own. I guess this exact line causes the problem.
To solve this, you have to disable the default change animation, which kills all the other animations on the list items views. Please note this will remove the default cross-fade.
((SimpleItemAnimator) RecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
I know the question is a year old, but maybe we can get the discussion going again. RecyclerView
still kills animation as of yet, and as I couldn't find any easy answer to this question I will gave it a try.
It is possible to animate inside the list using a Handler
for the MainThread
.
Handler mainHandler = new Handler(mContext.getMainLooper());
mainHandler.post(new Runnable() {
@Override
public void run() {
holder.mCheckBox.toggle();
}
});
This may be an overkill but works for any kind of animation, even inside onBindViewHolder
. I strongly suggest checking if the animation should play beforehand, or it will play even when scrolling.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With