Last active
December 7, 2017 12:53
-
-
Save antifriz/ad4412b72d50f63ec977 to your computer and use it in GitHub Desktop.
Quick workaround for animation bug related to SwitchCompat in RecyclerView, bug: https://code.google.com/p/android/issues/detail?id=196652
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package android.support.v7.widget; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
/** | |
* Created by ivan on 2/19/16. | |
* | |
* This is a quick workaround for SwitchCompat class. | |
* | |
* To enable animation during toggle, use toggleFromOutside method | |
* | |
* Any comment/improvement will be appreciated. | |
* It isn't tested much so there could be bugs. | |
*/ | |
public class SwitchCompatCompat extends SwitchCompat { | |
private boolean mIsCheckedFromOutside = true; | |
public SwitchCompatCompat(Context context) { | |
super(context); | |
} | |
public SwitchCompatCompat(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
@Override | |
public boolean isShown() { | |
return mIsCheckedFromOutside || super.isShown(); | |
} | |
/** | |
* replacement for toggle method which implements workaround for bug: | |
* https://code.google.com/p/android/issues/detail?id=196652 | |
*/ | |
public void toggleFromOutside() { | |
mIsCheckedFromOutside = true; | |
setChecked(!isChecked()); | |
} | |
@Override | |
public void setChecked(final boolean checked) { | |
if (mIsCheckedFromOutside) { | |
post(new Runnable() { | |
@Override | |
public void run() { | |
setChecked(checked); | |
} | |
}); | |
} else { | |
super.setChecked(checked); | |
} | |
} | |
@Override | |
public void jumpDrawablesToCurrentState() { | |
if(!mIsCheckedFromOutside){ | |
super.jumpDrawablesToCurrentState(); | |
} | |
} | |
@Override | |
protected void onAttachedToWindow() { | |
super.onAttachedToWindow(); | |
mIsCheckedFromOutside = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment