oligazar
8/8/2017 - 2:21 PM

SwitchImageView

SwitchImageView


class SwitchImageView: ImageView, Checkable {

    private var mChecked: Boolean = false
    var drawableOn: Drawable? = null
    var drawableOff: Drawable? = null

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int): super(context, attrs, defStyleAttr) {
        obtainStyledAttributes(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet): super(context, attrs) {
        obtainStyledAttributes(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int): super(context, attrs, defStyleAttr, defStyleRes) {
        obtainStyledAttributes(context, attrs)
    }

    private fun obtainStyledAttributes(context: Context, attrs: AttributeSet) {
        val a = context.obtainStyledAttributes(attrs, R.styleable.SwitchImageView, 0, 0)
        drawableOn = a.getDrawable(R.styleable.SwitchImageView_drawableOn)
        drawableOff = a.getDrawable(R.styleable.SwitchImageView_drawableOff)

        isChecked = a.getBoolean(R.styleable.SwitchImageView_checked, false)
        a.recycle()
    }

    // Checkable methods

    override fun isChecked() = mChecked

    override fun toggle() {
        isChecked = !mChecked
    }

    override fun setChecked(checked: Boolean) {
        if (mChecked != checked) {
            mChecked = checked
            refreshDrawableState()
        }
        syncDrawableState()
    }

    private fun syncDrawableState() {
        val checked = isChecked
        if (checked && drawableOn != null) {
            setImageDrawable(drawableOn)
        } else if (!checked && drawableOff != null) {
            setImageDrawable(drawableOff)
        }
    }

    override fun getAccessibilityClassName(): CharSequence {
        return SwitchImageView::class.java.name
    }

    // Saving view state
    internal class SavedState : View.BaseSavedState {
        var checked: Boolean = false

        /**
         * Constructor called from [CompoundButton.onSaveInstanceState]
         */
        constructor(superState: Parcelable) : super(superState)

        /**
         * Constructor called from [.CREATOR]
         */
        private constructor(parcel: Parcel) : super(parcel) {
            checked = parcel.readValue(null) as Boolean
        }

        override fun writeToParcel(out: Parcel, flags: Int) {
            super.writeToParcel(out, flags)
            out.writeValue(checked)
        }

        override fun toString(): String {
            return "CompoundButton.SavedState{${Integer.toHexString(System.identityHashCode(this))} checked= $checked }"
        }

        companion object {

            val CREATOR: Parcelable.Creator<SavedState> = object : Parcelable.Creator<SavedState> {
                override fun createFromParcel(`in`: Parcel): SavedState {
                    return SavedState(`in`)
                }

                override fun newArray(size: Int): Array<SavedState?> {
                    return arrayOfNulls(size)
                }
            }
        }
    }

    public override fun onSaveInstanceState(): Parcelable {
        val superState = super.onSaveInstanceState()

        val ss = SavedState(superState)

        ss.checked = isChecked
        return ss
    }

    public override fun onRestoreInstanceState(state: Parcelable) {
        val ss = state as SavedState

        super.onRestoreInstanceState(ss.superState)
        isChecked = ss.checked
        requestLayout()
    }
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="SwitchImageView">
        <attr name="drawableOn" format="string" />
        <attr name="drawableOff" format="string" />
        <attr name="checked" format="boolean" />
    </declare-styleable>
</resources>