我有一个 RecyclerView,我向其中传递文本并希望了解以下内容:如果 lineCount > 3,则我显示展开按钮,将椭圆大小设置为结束状态并使 maxLines = 3。
为此,我使用 textView 侦听器,如下所示。我设置 currentLineCount = textView.lineCount 并导致在侦听器内设置其他状态,例如 maxLines 等
fun View.afterLayoutConfiguration(func: () -> Unit) {
viewTreeObserver?.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
viewTreeObserver?.removeOnGlobalLayoutListener(this)
func()
}
})
}
但是,我遇到了问题,因为 RecyclerView 正在重用项目,并且监听会导致滚动时将旧视图的状态应用于新项目。
我需要帮助了解如何解决这种情况,因为我还没有找到如何计算文本视图中的行数。
目标是查明我的文本是否超过 3 行。如果是,则使按钮可见并将最大行数设置为3。行数侦听器的解决方案不正确,因为在recyclerView中它带有bug
我最终走上了一条不同的道路。我决定设置属性
android:ellipsize="end"
,android:maxLines="3"
然后
Text.post {
val isEllipsizeApplied = TextUtils.equals(text, Text.layout.text).not()
tvExpand.visibility = if (isEllipsizeApplied || isExpanded) View.VISIBLE else View.GONE
}
我希望这对某人有帮助)