Skip to content

Instantly share code, notes, and snippets.

@antifriz
Last active December 7, 2017 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antifriz/ad4412b72d50f63ec977 to your computer and use it in GitHub Desktop.
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
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