Merge branch 'dev' of gitlab.ghzs.com:halo/assistant-android into dev

This commit is contained in:
Jack
2020-03-09 15:59:03 +08:00

View File

@ -2,6 +2,7 @@ package com.gh.gamecenter.game.horizontal
import android.text.TextUtils
import android.view.Gravity
import android.view.ViewTreeObserver
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.gh.base.BaseRecyclerViewHolder
@ -15,27 +16,30 @@ class GameHorizontalItemViewHolder(val binding: GameHorizontalItemBinding) : Bas
@BindingAdapter("setHorizontalNameAndGravity")
fun setHorizontalNameAndGravity(view: TextView, name: String?) {
view.text = name
view.post {
view.gravity = if (view.lineCount >= 2) {
Gravity.START
} else {
Gravity.CENTER
view.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener{
override fun onGlobalLayout() {
view.viewTreeObserver.removeOnGlobalLayoutListener(this);
view.gravity = if (view.lineCount >= 2) {
Gravity.START
} else {
Gravity.CENTER
}
val newText = autoSplitText(view);
if (!TextUtils.isEmpty(newText)) {
view.text = newText;
}
}
val newText = autoSplitText(view)
if (!TextUtils.isEmpty(newText)) {
view.text = newText
}
view.requestLayout()
}
})
}
// 手动把文本进行换行处理
private fun autoSplitText(tv: TextView): String {
private fun autoSplitText(tv: TextView): String? {
try {
val rawText = tv.text.toString()
val tvPaint = tv.paint
val tvWidth = (tv.width - tv.paddingLeft - tv.paddingRight).toFloat()
val width = tv.width
if (width == 0) return null // 为零时,执行以下代码会进入死循环
val tvWidth = (width - tv.paddingLeft - tv.paddingRight).toFloat()
val rawTextLines = rawText
.replace("\r".toRegex(), "")
.split("\n".toRegex())