package com.xwray.groupie.example.widget;
import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.widget.Checkable;
public class CheckableImageView extends AppCompatImageView implements Checkable {
public CheckableImageView(Context context) {
super(context);
}
public CheckableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private static final int[] CheckedStateSet = {android.R.attr.state_checked};
private boolean checked = false;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean b) {
if (b != checked) {
checked = b;
refreshDrawableState();
}
}
public void toggle() {
setChecked(!checked);
}
@Override
public int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CheckedStateSet);
}
return drawableState;
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
invalidate();
}
}