@@ -14,7 +14,6 @@ import android.view.View
1414import android.widget.LinearLayout
1515import androidx.annotation.Px
1616import androidx.core.content.res.ResourcesCompat
17- import androidx.core.view.isVisible
1817import kotlinx.android.synthetic.main.expandable_textview_layout.view.*
1918
2019class ExpandableTextView @JvmOverloads constructor(
@@ -25,17 +24,24 @@ class ExpandableTextView @JvmOverloads constructor(
2524
2625 var stateChangeListener: OnStateChangeListener ? = null
2726
28- var currentState = State .EXPANDED
27+ var currentState = State .COLLAPSED
2928 private set(value) {
3029 field = value
30+ when (value) {
31+ State .EXPANDED -> {
32+ tv_origin_text.maxLines = Int .MAX_VALUE
33+ view_gradient.visibility = View .GONE
34+ }
35+ State .COLLAPSED -> {
36+ tv_origin_text.maxLines = collapsedLines
37+ view_gradient.visibility = View .VISIBLE
38+ }
39+ }
3140 stateChangeListener?.onStateChanged(value)
3241 }
3342
34- private var isActivateExpansion = false
35- set(value) {
36- field = value
37- view_gradient.isVisible = value
38- }
43+ var isActivateExpansion = false
44+ private set
3945
4046 val isExpanded
4147 get() = currentState == State .EXPANDED
@@ -155,29 +161,32 @@ class ExpandableTextView @JvmOverloads constructor(
155161 }
156162 }
157163
158- private fun updateText (value : String ) {
164+ private fun updateText (value : String? ) {
159165 tv_origin_text.maxLines = Int .MAX_VALUE
160166 tv_origin_text.text = value
161167 isActivateExpansion = tv_origin_text.lineCount > collapsedLines
162- toggle()
168+
169+ if (! isActivateExpansion || isExpanded || tv_origin_text.text.isNullOrEmpty()) {
170+ view_gradient.visibility = View .GONE
171+ return
172+ }
173+
174+ tv_origin_text.maxLines = collapsedLines
175+ view_gradient.visibility = View .VISIBLE
163176 }
164177
165178 fun expand () {
166179 if (! isActivateExpansion || isExpanded || text.isEmpty()) {
167180 return
168181 }
169182 currentState = State .EXPANDED
170- tv_origin_text.maxLines = Int .MAX_VALUE
171- view_gradient.visibility = View .GONE
172183 }
173184
174185 fun collapse () {
175186 if (! isActivateExpansion || isCollapsed || text.isEmpty()) {
176187 return
177188 }
178189 currentState = State .COLLAPSED
179- tv_origin_text.maxLines = collapsedLines
180- view_gradient.visibility = View .VISIBLE
181190 }
182191
183192 enum class State {
0 commit comments