Created
May 14, 2012 20:21
-
-
Save JakeWharton/2696476 to your computer and use it in GitHub Desktop.
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
public class CheckableFrameLayout extends FrameLayout implements Checkable { | |
private static final int[] CHECKED_STATE_SET = { | |
android.R.attr.state_activated, | |
android.R.attr.state_checked, | |
}; | |
private boolean mChecked; | |
public CheckableFrameLayout(Context context) { | |
super(context); | |
} | |
public CheckableFrameLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public CheckableFrameLayout(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
@Override | |
protected int[] onCreateDrawableState(int extraSpace) { | |
final int[] drawableState = super.onCreateDrawableState(extraSpace + 2); | |
if (mChecked) { | |
mergeDrawableStates(drawableState, CHECKED_STATE_SET); | |
} | |
return drawableState; | |
} | |
@Override | |
public void setChecked(boolean checked) { | |
if (mChecked != checked) { | |
mChecked = checked; | |
invalidate(); | |
refreshDrawableState(); | |
} | |
} | |
@Override | |
public boolean isChecked() { | |
return mChecked; | |
} | |
@Override | |
public void toggle() { | |
setChecked(!mChecked); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment