You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tasks/src/main/java/com/todoroo/astrid/ui/CheckableImageView.java

87 lines
2.1 KiB
Java

/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.astrid.ui;
import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.ViewDebug;
import android.widget.Checkable;
public class CheckableImageView extends AppCompatImageView implements Checkable {
private static final int[] CHECKED_STATE_SET = {
android.R.attr.state_checked
};
public CheckableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean performClick() {
toggle();
return super.performClick();
}
private boolean mChecked;
private boolean mBroadcasting;
@Override
public void toggle() {
setChecked(!mChecked);
}
@Override
@ViewDebug.ExportedProperty
public boolean isChecked() {
return mChecked;
}
/**
* <p>Changes the checked state of this button.</p>
*
* @param checked true to check the button, false to uncheck it
*/
@Override
public void setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
refreshDrawableState();
// Avoid infinite recursions if setChecked() is called from a listener
if (mBroadcasting) {
return;
}
mBroadcasting = false;
}
}
@Override
public int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (getDrawable() != null) {
int[] myDrawableState = getDrawableState();
// Set the state of the Drawable
getDrawable().setState(myDrawableState);
invalidate();
}
}
}