Implemented that if count lines is <= count max displayed lines in collapsed mode, not expand button gets displayed
This commit is contained in:
parent
2b0a1e9e14
commit
ddf2336ed5
|
@ -3,15 +3,26 @@ package net.dankito.banking.ui.android.views
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
import android.widget.ScrollView
|
import android.widget.ScrollView
|
||||||
import kotlinx.android.synthetic.main.view_collapsible_text.view.*
|
import kotlinx.android.synthetic.main.view_collapsible_text.view.*
|
||||||
import net.dankito.banking.ui.android.R
|
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(
|
open class CollapsibleTextView @JvmOverloads constructor(
|
||||||
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
|
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
|
||||||
) : ScrollView(context, attrs, defStyleAttr) {
|
) : ScrollView(context, attrs, defStyleAttr) {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
const val CountDisplayedLinesWhenCollapsed = 3
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
protected var isCollapsed = true
|
protected var isCollapsed = true
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,8 +34,14 @@ open class CollapsibleTextView @JvmOverloads constructor(
|
||||||
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
||||||
val rootView = inflater.inflate(R.layout.view_collapsible_text, this, true)
|
val rootView = inflater.inflate(R.layout.view_collapsible_text, this, true)
|
||||||
|
|
||||||
rootView.txtvwCollapsibleText.setOnClickListener { toggleIsCollapsed() }
|
rootView.apply {
|
||||||
rootView.btnExpandCollapseTextView.setOnClickListener { toggleIsCollapsed() }
|
txtvwCollapsibleText.setOnClickListener { toggleIsCollapsed() }
|
||||||
|
btnExpandCollapseTextView.setOnClickListener { toggleIsCollapsed() }
|
||||||
|
|
||||||
|
txtvwCollapsibleText.addTextChangedListener(StandardTextWatcher {
|
||||||
|
checkIfExpandCollapseButtonShouldBeDisplayed(context)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,11 +53,20 @@ open class CollapsibleTextView @JvmOverloads constructor(
|
||||||
isCollapsed = false
|
isCollapsed = false
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
txtvwCollapsibleText.maxLines = 3
|
txtvwCollapsibleText.maxLines = CountDisplayedLinesWhenCollapsed
|
||||||
btnExpandCollapseTextView.setImageResource(R.drawable.ic_baseline_expand_more_24)
|
btnExpandCollapseTextView.setImageResource(R.drawable.ic_baseline_expand_more_24)
|
||||||
|
|
||||||
isCollapsed = true
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue