-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(module-test): add custom checkable
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
test/src/main/java/com/neo/test/checkable/CustomCheckable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters