设置新值时,“wrap_content”在 MotionLayout 中无法正常工作。我在 RecyclerView 上方创建了一个带有 TextView 的示例。TextView 最初设置为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>
更新:implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3
没有这个问题。