RecyclerView 高度 wrap_content 失效的问题

本文详细介绍了在遇到RecyclerView设置高度为wrap_content时失效的问题,特别是当使用GridLayoutManager且只显示一行数据的情况。作者指出这可能是谷歌的一个BUG,并提供了一个有效的修复方案,通过在RecyclerView外层包裹一个布局,将RecyclerView的高度设置为外层布局的高度来解决问题。

转载请注明链接:https://blog.youkuaiyun.com/feather_wch/article/details/88293585

试过很多办法,但是都是标题党。本问题绝对可以解决。

RecyclerView 高度 wrap_content 失效的问题

版本:2019-03-07(12:30)

RecyclerView的属性android:layout_height="wrap_content"出现了失效的问题,我是用的GridLayoutManager,结果只有一行的数据。

该问题是谷歌的BUG

修复方法

1、修复方法

  1. RelativeLayout包裹RecyclerView,RelativeLayout的高度采用wrap_content
  2. RecyclerView的高度,采用match_parent

如下:

<RelativeLayout
    
<!-- 明细行布局 --> <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="4dp" android:background="@{position % 2 == 0 ? @color/white : @color/gray_100}"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="8dp"> <!-- 物料编码 --> <TextView android:id="@+id/tvMaterialCode" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.5" android:text="MAT-001"/> <!-- 物料名称 --> <TextView android:id="@+id/tvMaterialName" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="物料A"/> <!-- 规格 --> <TextView android:id="@+id/tvSpecification" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.5" android:text="标准"/> <!-- 数量 --> <TextView android:id="@+id/tvQuantity" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="100"/> <!-- 单价 --> <TextView android:id="@+id/tvUnitPrice" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="10.00"/> <!-- 金额 --> <TextView android:id="@+id/tvAmount" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="1000.00"/> <!-- 编辑按钮 --> <ImageButton android:id="@+id/btnEdit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.5" android:src="@drawable/ic_edit"/> </LinearLayout> </androidx.cardview.widget.CardView> 帮我生成一下上面代码的预览结果
最新发布
07-30
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猎羽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值