Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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