@@ -9,6 +9,7 @@ import android.util.TypedValue
9
9
import android.view.LayoutInflater
10
10
import android.view.View
11
11
import android.widget.RelativeLayout
12
+ import androidx.annotation.RestrictTo
12
13
import androidx.appcompat.app.AppCompatDelegate
13
14
import androidx.core.content.ContextCompat
14
15
import kotlinx.android.synthetic.main.layout_checkable_text.view.*
@@ -79,15 +80,18 @@ class CheckableTextView : RelativeLayout {
79
80
}
80
81
81
82
animateView(checkedIV, isChecked)
82
- rootRL.setOnClickListener {
83
+ rootRL.setOnClickListener(clickListener())
84
+ }
85
+
86
+ private fun clickListener (): (v: View ) -> Unit {
87
+ return {
83
88
checkedTextTV.text = checkedTextTV.text
84
89
checkedTextTV.isSelected = true
85
90
isChecked = ! isChecked
86
91
animateView(checkedIV, isChecked)
87
92
notifyListener(isChecked)
88
93
}
89
94
}
90
-
91
95
private fun applyTextStyle (textStyle : Int , context : Context ) {
92
96
if (isValidRes(textStyle)) {
93
97
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
@@ -125,19 +129,46 @@ class CheckableTextView : RelativeLayout {
125
129
listener?.onCheckChange(this , isChecked)
126
130
}
127
131
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
+
128
158
fun setOnCheckChangeListener (listener : CheckedListener ) {
129
159
this .listener = listener
130
160
}
131
161
132
- fun setChecked (isChecked : Boolean , shouldNotifyListeners : Boolean ) {
162
+ fun setChecked (isChecked : Boolean , shouldNotifyListeners : Boolean =false ) {
133
163
this .isChecked = isChecked
134
164
animateView(checkedIV, isChecked)
135
165
if (shouldNotifyListeners)
136
166
notifyListener(isChecked)
137
167
}
138
168
139
- fun setChecked (isChecked : Boolean ) {
140
- setChecked(isChecked, false )
169
+
170
+ fun isChecked (): Boolean {
171
+ return this .isChecked
141
172
}
142
173
143
174
interface CheckedListener {
0 commit comments