@@ -9,6 +9,7 @@ import android.util.TypedValue
99import android.view.LayoutInflater
1010import android.view.View
1111import android.widget.RelativeLayout
12+ import androidx.annotation.RestrictTo
1213import androidx.appcompat.app.AppCompatDelegate
1314import androidx.core.content.ContextCompat
1415import kotlinx.android.synthetic.main.layout_checkable_text.view.*
@@ -79,15 +80,18 @@ class CheckableTextView : RelativeLayout {
7980 }
8081
8182 animateView(checkedIV, isChecked)
82- rootRL.setOnClickListener {
83+ rootRL.setOnClickListener(clickListener())
84+ }
85+
86+ private fun clickListener (): (v: View ) -> Unit {
87+ return {
8388 checkedTextTV.text = checkedTextTV.text
8489 checkedTextTV.isSelected = true
8590 isChecked = ! isChecked
8691 animateView(checkedIV, isChecked)
8792 notifyListener(isChecked)
8893 }
8994 }
90-
9195 private fun applyTextStyle (textStyle : Int , context : Context ) {
9296 if (isValidRes(textStyle)) {
9397 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
@@ -125,19 +129,46 @@ class CheckableTextView : RelativeLayout {
125129 listener?.onCheckChange(this , isChecked)
126130 }
127131
132+
133+ private fun getRippleDrawable (): Int {
134+ val outValue = TypedValue ()
135+ context.theme.resolveAttribute(android.R .attr.selectableItemBackground, outValue, true )
136+ return outValue.resourceId
137+ }
138+
139+
140+ /* -------------------------------------------------public functions------------------------------------------------------------------------------------------*/
141+
142+
143+
144+ /* *
145+ * Change [CheckableTextView] click state
146+ * @param isClickable = pass true for enable clicks and false for disable clicks.
147+ */
148+ @RestrictTo(RestrictTo .Scope .TESTS )
149+ fun setClickEnabled (isClickable : Boolean ) {
150+ // 0.5 second delay added to ongoing ripple animation to complete (if any)
151+ rootRL.postDelayed(
152+ { rootRL.setBackgroundResource(if (isClickable) getRippleDrawable() else android.R .color.transparent) },
153+ 500
154+ )
155+ rootRL.setOnClickListener(if (isClickable) clickListener() else null )
156+ }
157+
128158 fun setOnCheckChangeListener (listener : CheckedListener ) {
129159 this .listener = listener
130160 }
131161
132- fun setChecked (isChecked : Boolean , shouldNotifyListeners : Boolean ) {
162+ fun setChecked (isChecked : Boolean , shouldNotifyListeners : Boolean =false ) {
133163 this .isChecked = isChecked
134164 animateView(checkedIV, isChecked)
135165 if (shouldNotifyListeners)
136166 notifyListener(isChecked)
137167 }
138168
139- fun setChecked (isChecked : Boolean ) {
140- setChecked(isChecked, false )
169+
170+ fun isChecked (): Boolean {
171+ return this .isChecked
141172 }
142173
143174 interface CheckedListener {
0 commit comments