Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change CheckBox checked with animation in RecyclerView

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.

like image 532
WonderCsabo Avatar asked Dec 18 '15 16:12

WonderCsabo


2 Answers

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);
like image 97
WonderCsabo Avatar answered Oct 23 '22 05:10

WonderCsabo


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.

like image 38
Raeglan Avatar answered Oct 23 '22 03:10

Raeglan