android - RecyclerView 中具有最大宽度的简单约束布局子项

标签 android android-layout android-constraintlayout

我正在尝试创建一个 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>

enter image description here




但是,如果我在下面的 onBindViewHolder() 中调用 TextView 上的 requestLayout(),请注意它是如何工作的:

更新:onBindViewHolder() 中的以下代码也有效。

ConstraintSet().apply {
    clone(holder.parent)
    applyTo(holder.parent)
}

父级是 ConstraintLayout

enter image description here

有人知道这里发生了什么吗?这是 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/

相关文章:

java - 当子值匹配时,Firebase 对所有子条目求和

java - 在 AlertDialog 中调用方法时出错

android - 如何在 Android Jetpack Compose 中向 Card 对象添加图像背景?

安卓 : ConstraintLayout : layout_constraintVertical_bias not working as expected

java - 如何在约束布局中以编程方式移动 View (按钮)?

android - 对话框使 Android 服务崩溃

android - 3G 中的 NAT 是如何工作的

java - 用 subview 网格填充屏幕,但 subview 的大小是预期大小的一半

Java缩放图像

android - Jetpack Compose 中是否有类似 ConstraintLayout "dimensionRatio"属性?