Android实现RecyclerView条目随内容自适应高度且支持侧滑功能
问题根因
两个布局规则错误直接导致适配失效:
- 副标题
tv_notification_desc设置了android:layout_height="0dp"(约束匹配模式),同时绑定了上下边界约束,当标题文本过长换行时,ConstraintLayout会优先压缩该控件高度,直接裁剪副标题内容 - 前台卡片外层套了冗余的FrameLayout,导致SwipeRevealLayout测量前后层高度时出现层级偏差,侧滑露出的后台操作区无法获取前台条目的实际渲染高度,无法同步高度
纯XML布局修改方案
无需编写任何动态计算高度的代码,仅调整布局规则即可实现完全自适应:
- 删除前台CardView外层冗余的FrameLayout,让前后层布局直接作为SwipeRevealLayout的直接子View,保证测量逻辑正常
- 将副标题
tv_notification_desc的高度属性改为wrap_content,移除绑定父布局底部的约束,仅保留顶部对齐标题、左右对齐标题边距的约束,让副标题随文本内容自动撑开高度 - 清理CardView上无效的ConstraintLayout约束属性(原父布局为FrameLayout,这些属性本身不生效)
修改后的完整布局代码如下:
<?xml version="1.0" encoding="utf-8"?> <com.voiro.phoenix.custom.swipe_layout.SwipeRevealLayout 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:id="@+id/swipe_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:dragFromEdge="right"> <!-- 侧滑后台操作区 --> <RelativeLayout android:id="@+id/rl_mark_read" android:layout_width="100dp" android:layout_height="match_parent" android:background="@color/green" android:clickable="true" android:focusable="true"> <TextView android:id="@+id/tv_mark_read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:drawablePadding="2dp" android:fontFamily="@font/roboto_medium" android:gravity="center" android:text="@string/mark_as_read" android:textColor="@color/white" android:textSize="12sp" app:drawableTopCompat="@drawable/ic_double_check" /> </RelativeLayout> <!-- 前台内容区 移除了原外层冗余FrameLayout --> <androidx.cardview.widget.CardView android:id="@+id/view_foreground" android:layout_width="match_parent" android:layout_height="wrap_content" app:cardBackgroundColor="@color/background"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/cl_main_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="90dp" android:padding="16dp"> <ImageView android:id="@+id/iv_notification_thumbnail" android:layout_width="36dp" android:layout_height="36dp" android:src="@drawable/ic_avatar" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:ignore="ContentDescription" /> <TextView android:id="@+id/tv_notification_title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginEnd="8dp" android:fontFamily="@font/roboto" android:text="@string/value_place_holder" android:textColor="@color/white" android:textSize="14sp" app:layout_constraintEnd_toStartOf="@id/tv_notification_time" app:layout_constraintStart_toEndOf="@id/iv_notification_thumbnail" app:layout_constraintTop_toTopOf="@id/iv_notification_thumbnail" /> <TextView android:id="@+id/tv_notification_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="@font/roboto" android:text="@string/value_place_holder" android:textColor="@color/text_desc" android:textSize="12sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="@id/tv_notification_title" /> <TextView android:id="@+id/tv_notification_desc" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:fontFamily="@font/roboto_medium" android:text="@string/value_place_holder" android:textColor="@color/text_desc" android:textSize="10sp" app:layout_constraintEnd_toEndOf="@id/tv_notification_title" app:layout_constraintStart_toStartOf="@id/tv_notification_title" app:layout_constraintTop_toBottomOf="@id/tv_notification_title" tools:ignore="SmallSp" /> <ImageButton android:id="@+id/ib_unread_dot" android:layout_width="8dp" android:layout_height="8dp" android:background="@drawable/shape_circle_orange" android:clickable="false" android:visibility="visible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" tools:ignore="ContentDescription" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> </com.voiro.phoenix.custom.swipe_layout.SwipeRevealLayout>
效果说明
- 短文本场景下,条目自动保持90dp的最小高度,和系统通知栏样式一致
- 长文本场景下,标题自动换行,副标题完整显示不被裁剪,条目总高度随内容自动延展
- 侧滑露出的后台操作区高度始终和前台条目高度完全一致,无需动态计算
效果参考:
- 预期效果:

- 修改前异常效果:

内容的提问来源于stack exchange,提问作者Ashish M
相关产品推荐
相关产品推荐

