package com.zztrapp.zztrapp.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Checkable;

import androidx.annotation.Nullable;

/**
 * @ProjectName: TrdczkApp
 * @ClassName: ImageViewCheckable
 * @Author: zhangping
 * @CreateDate: 2021/10/6 10:17
 * @Description: 描述说明
 */
public class ImageViewCheckable extends androidx.appcompat.widget.AppCompatImageView implements Checkable {
    private boolean mChecked;


    private static final int[] CHECK_STATE_SET = {android.R.attr.state_checked};

    public ImageViewCheckable(Context context) {
        super(context);
    }

    public ImageViewCheckable(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ImageViewCheckable(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public void setChecked(boolean checked) {
        if (mChecked != checked) {
            mChecked = checked;
            refreshDrawableState();//刷新drawable状态
        }
    }

    /**
     * Imageview默认不支持state_checked状态，需要重写此方法
     *
     * @param extraSpace
     * @return
     */
    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CHECK_STATE_SET);
        }
        return drawableState;
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }
}
