androidandroid-recyclerviewandroid-constraintlayoutconstraintset

Change constraint in recyclerview viewholder layout - android


I am working on chat app so I want time TextView to be like whats app it's constraint can change depend on the text

So I tried the view tree observer in the message layout and it somehow worked but there something missing

 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)

                        }
                    }
                }
            }

        })
    }

and here is my layout 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>

The problem is that when I open an existing chat the constraint as first image appear didn't work as expected in the long message but as soon as I click on type a message edit text the constraint changed for the screen chat not for all chat so if i scrolled the constraints of chat on screen is unchanged unless i click on type a message edit text and so on.

am i missing listener or request focus or something else or what is missing

Here is picture one https://i.sstatic.net/lBdEl.png

Here is picture two enter image description here


Solution

  • it worked by make recyclerView request layout

    i added this code to recyclerview adapter

      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()
            }
        })
    }