我正在尝试创建一个 TextView
以使其包装其内容直到达到其最大宽度。以下代码在 RecyclerView
中使用之前可以完美地实现这一点。在 onBindViewHolder()
中更改其文本后,TextView
似乎没有再次测量其宽度。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/message"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_max="wrap"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.72" />
</androidx.constraintlayout.widget.ConstraintLayout>
但是,如果我在下面的 onBindViewHolder()
中调用 TextView
上的 requestLayout()
,请注意它是如何工作的:
更新:onBindViewHolder()
中的以下代码也有效。
ConstraintSet().apply {
clone(holder.parent)
applyTo(holder.parent)
}
父级是 ConstraintLayout
。
有人知道这里发生了什么吗?这是 ConstraintLayout
中的错误吗?在 onBindViewHolder()
中调用 requestLayout()
是一种不好的做法,我不想那样做。
最佳答案
我知道晚了一个月,但我最近遇到了这个问题,也找不到答案。作者提出的不断变化的 ConstraintSet() 有效,但始终要考虑这是否有效。另一种解决方案是将 app:layout_constraintWidth_max="wrap"
更改为 app:layout_constraintWidth_default="wrap"
这对我有用,recyclerView 在回收 View 持有者时正确更新约束
关于android - RecyclerView 中具有最大宽度的简单约束布局子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58281419/