Implemented that if count lines is <= count max displayed lines in collapsed mode, not expand button gets displayed

This commit is contained in:
dankito 2020-09-10 16:11:14 +02:00
parent 2b0a1e9e14
commit ddf2336ed5
1 changed files with 29 additions and 3 deletions

View File

@ -3,15 +3,26 @@ package net.dankito.banking.ui.android.views
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ScrollView
import kotlinx.android.synthetic.main.view_collapsible_text.view.*
import net.dankito.banking.ui.android.R
import net.dankito.banking.ui.android.util.StandardTextWatcher
import net.dankito.utils.android.extensions.asActivity
import java.util.*
import kotlin.concurrent.schedule
open class CollapsibleTextView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ScrollView(context, attrs, defStyleAttr) {
companion object {
const val CountDisplayedLinesWhenCollapsed = 3
}
protected var isCollapsed = true
@ -23,8 +34,14 @@ open class CollapsibleTextView @JvmOverloads constructor(
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val rootView = inflater.inflate(R.layout.view_collapsible_text, this, true)
rootView.txtvwCollapsibleText.setOnClickListener { toggleIsCollapsed() }
rootView.btnExpandCollapseTextView.setOnClickListener { toggleIsCollapsed() }
rootView.apply {
txtvwCollapsibleText.setOnClickListener { toggleIsCollapsed() }
btnExpandCollapseTextView.setOnClickListener { toggleIsCollapsed() }
txtvwCollapsibleText.addTextChangedListener(StandardTextWatcher {
checkIfExpandCollapseButtonShouldBeDisplayed(context)
})
}
}
@ -36,11 +53,20 @@ open class CollapsibleTextView @JvmOverloads constructor(
isCollapsed = false
}
else {
txtvwCollapsibleText.maxLines = 3
txtvwCollapsibleText.maxLines = CountDisplayedLinesWhenCollapsed
btnExpandCollapseTextView.setImageResource(R.drawable.ic_baseline_expand_more_24)
isCollapsed = true
}
}
protected open fun checkIfExpandCollapseButtonShouldBeDisplayed(context: Context) {
Timer().schedule(500) { // wait some time till txtvwCollapsibleText is layout and lineCount is set
context.asActivity()?.runOnUiThread {
val showExpandButton = isCollapsed == false || txtvwCollapsibleText.lineCount > CountDisplayedLinesWhenCollapsed
btnExpandCollapseTextView.visibility = if (showExpandButton) View.VISIBLE else View.GONE
}
}
}
}