更改 recyclerview viewholder 布局中的约束 - android

Change constraint in recyclerview viewholder layout - android

我正在开发聊天应用程序,所以我希望时间 TextView 像什么应用程序一样它的约束可以根据文本改变

所以我在消息布局中尝试了视图树观察器,它以某种方式工作但缺少一些东西

 private fun adjustTimeTextView() {

        var freeSpace: Float
        var lines: Int
        val messageViewObserver = messageView.messageLayout.viewTreeObserver
        messageViewObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {

            override fun onGlobalLayout() {

                messageView.messageLayout.viewTreeObserver.removeOnGlobalLayoutListener(this)
                val viewWidth = messageView.messageLayout.width
                Log.e("message view width", viewWidth.toString())
                val layout = messageView.messageTextTextView.layout

                if (layout != null) {
                    lines = layout.lineCount
                    Log.e("line", lines.toString())

                    val offset = layout.getLineWidth(lines - 1)

                    freeSpace = viewWidth - offset
                    if (freeSpace < 220) {
                        Log.e("minmum", "low free space")
                        val constraintSet = ConstraintSet()
                        constraintSet.clone(messageView.messageLayout)
                        constraintSet.clear(messageView.messageLayout.messageTimeTextView.id, ConstraintSet.TOP)
                        constraintSet.clear(messageView.messageLayout.messageTimeTextView.id, ConstraintSet.BOTTOM)
                        constraintSet.clear(messageView.messageLayout.messageTimeTextView.id, ConstraintSet.START)
                        constraintSet.connect(
                            messageView.messageLayout.messageTimeTextView.id,
                            ConstraintSet.TOP,
                            messageView.messageLayout.messageTextTextView.id,
                            ConstraintSet.BOTTOM
                        )
                        constraintSet.applyTo(messageView.messageLayout)

                    } else {
                        if (lines > 1) {
                            val constraintSet = ConstraintSet()
                            constraintSet.clone(messageView.messageLayout)
                            constraintSet.clear(
                                messageView.messageLayout.messageTimeTextView.id,
                                ConstraintSet.START
                            )
                            constraintSet.applyTo(messageView.messageLayout)

                        }
                    }
                }
            }

        })
    }

这是我的布局 xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>


<android.support.constraint.Guideline
    android:id="@+id/endGuideline"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintGuide_percent="0.65"/>

<android.support.constraint.Guideline
    android:id="@+id/startGuideline"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintGuide_percent="0.35"/>

<android.support.constraint.ConstraintLayout
    android:id="@+id/messageLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constrainedWidth="true"
    android:padding="4dp"
    app:layout_constraintEnd_toStartOf="@+id/endGuideline"
    android:background="@drawable/sender_message_background"
    android:orientation="vertical"
    app:layout_constraintWidth_default="wrap"
    app:layout_constraintStart_toStartOf="parent" app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintTop_toBottomOf="@+id/messageDateTextView" android:layout_marginTop="8dp">


    <TextView
        android:id="@+id/messageTextTextView"
        tools:text="hi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0" android:layout_marginEnd="8dp"/>

    <TextView
        android:id="@+id/messageTimeTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="12:34 pm"
        android:textSize="12sp"
        app:layout_constraintBottom_toBottomOf="@+id/messageTextTextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/messageTextTextView"
        app:layout_constraintStart_toEndOf="@+id/messageTextTextView"
        android:layout_marginStart="8dp" app:layout_constraintVertical_bias="0.51"/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

问题是,当我打开一个现有的聊天时,第一个图像出现的约束在长消息中没有按预期工作,但是当我点击键入消息编辑文本时,屏幕聊天的约束发生了变化不适用于所有聊天,所以如果我滚动屏幕上的聊天限制不变,除非我单击键入消息编辑文本等。

我是否缺少听众或请求焦点或其他东西或缺少什么

这是图片一

这是图二

它通过 make recyclerView 请求布局工作

我将此代码添加到 recyclerview 适配器

  override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
    super.onAttachedToRecyclerView(recyclerView)
    recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener(){
        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
            recyclerView.requestLayout()
        }
    })
}

在我看来,这里的解决方案是使用app:layout_constrainedWidth="true"。请求布局是一项繁重的工作,不应该在滚动上执行。