如何在Android Motion Layout中更改内容后使"wrap_content"工作?



设置新值时,"wrap_content"在 MotionLayout 中无法正常工作。我在回收器视图上方创建了一个带有文本视图的示例。文本视图最初设置为android:layout_height="wrap_content"android:text='Hello World!。然后TextView#setText()从服务器获取内容后将被调用。所以我的问题是,在我设置一些很长的文本后,TextView 高度不会改变。

我正在使用implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha4.下面是示例代码:

MainActivity.kt:

package com.paranoid.mao.motionlayoutwrapcontentexp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import java.util.concurrent.TimeUnit
import kotlin.concurrent.thread
class MainActivity : AppCompatActivity() {
    private val dummyItems = List(100) { "Item: $it" }
    private val longText = "Wrap content nnot works!"
    private lateinit var header: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        header = findViewById(R.id.header)
        val recyclerView = findViewById<RecyclerView>(R.id.recycler)
        recyclerView.adapter = MyAdapter()
        // set immediately will work fine
        // header.text = longText
        // set with some delay won't show properly
        simulateLoadFromRemote()
    }
    private fun simulateLoadFromRemote() {
        thread {
            TimeUnit.SECONDS.sleep(1)
            runOnUiThread {
                header.text = longText
            }
        }
    }
    ...
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout
    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="match_parent"
    app:layoutDescription="@xml/scene_activity_main"
    tools:motionProgress="0"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:paddingTop="48dp"
        android:text="Hello World!"
        android:textSize="48sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintTop_toBottomOf="@id/header"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.motion.widget.MotionLayout>

scene_activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<MotionScene
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">
    <Transition
        motion:constraintSetStart="@id/start"
        motion:constraintSetEnd="@id/end">
        <OnSwipe
            motion:dragDirection="dragUp"
            motion:touchAnchorId="@id/recycler"
            motion:touchAnchorSide="top"/>
    </Transition>
    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@id/header"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:scaleX="1"
            android:scaleY="1"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintTop_toTopOf="parent">
        </Constraint>
    </ConstraintSet>
    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@id/header"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:scaleX="0.5"
            android:scaleY="0.5"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintBottom_toTopOf="parent">
        </Constraint>
    </ConstraintSet>
</MotionScene>

预期:https://ibb.co/zR7Pq8V

实际:https://ibb.co/QdLkJQX

更新implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3没有这个问题。

最后,这个问题似乎在最新版本中得到解决2.0.0-beta7

相关内容

  • 没有找到相关文章

最新更新