diff --git a/test/src/main/java/com/neo/test/checkable/CustomCheckable.kt b/test/src/main/java/com/neo/test/checkable/CustomCheckable.kt new file mode 100644 index 00000000..b5d3a6a4 --- /dev/null +++ b/test/src/main/java/com/neo/test/checkable/CustomCheckable.kt @@ -0,0 +1,60 @@ +package com.neo.test.checkable + +import android.content.Context +import android.util.AttributeSet +import android.view.accessibility.AccessibilityNodeInfo +import android.widget.Checkable +import androidx.appcompat.widget.AppCompatTextView + +class CustomCheckable( + context: Context, + attrs: AttributeSet? = null, +) : AppCompatTextView(context, attrs), Checkable { + + private var checked = false + + private val CHECKED_STATE_SET = intArrayOf( + android.R.attr.state_checked + ) + + init { + isClickable = true + } + + override fun onInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfo) { + super.onInitializeAccessibilityNodeInfo(info) + + info.isCheckable = true + info.isChecked = isChecked + } + + override fun setChecked(checked: Boolean) { + this.checked = checked + + refreshDrawableState() + } + + override fun isChecked(): Boolean { + return checked + } + + override fun toggle() { + isChecked = !isChecked + } + + override fun performClick(): Boolean { + toggle() + + return super.performClick() + } + + override fun onCreateDrawableState(extraSpace: Int): IntArray? { + val drawableState = super.onCreateDrawableState(extraSpace + 1) + + if (isChecked) { + mergeDrawableStates(drawableState, CHECKED_STATE_SET) + } + + return drawableState + } +} \ No newline at end of file diff --git a/test/src/main/res/layout/fragment_switches.xml b/test/src/main/res/layout/fragment_switches.xml index a985b435..82cea40a 100644 --- a/test/src/main/res/layout/fragment_switches.xml +++ b/test/src/main/res/layout/fragment_switches.xml @@ -119,6 +119,32 @@ + + + + + + + + + + \ No newline at end of file