You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RecyclerView getItemCount返回正确数量但无条目显示问题排查

RecyclerView显示空白(getItemCount返回正确但无条目)问题排查与修复

核心问题定位

你的代码逻辑(Adapter绑定、数据添加、notifyDataSetChanged调用)都是正确的,问题出在条目布局result_list_rv.xml的布局参数设置错误,导致条目无法正常渲染显示。

具体修复步骤

1. 修改条目根布局宽度

将result_list_rv.xml的根LinearLayout的android:layout_width从wrap_content改为match_parent:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent" <!-- 此处修改为match_parent -->
    android:layout_height="wrap_content">

原wrap_content设置会导致根布局宽度计算异常,配合内部CardView的match_parent设置,最终条目宽度为0,视觉上完全空白。

2. 修复RelativeLayout高度

将条目布局中最后一个RelativeLayout的android:layout_height从match_parent改为wrap_content:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" <!-- 此处修改为wrap_content -->
    android:layout_marginEnd="20dp"
    android:layout_marginStart="20dp"
    android:layout_marginTop="20dp"
    android:gravity="center"
    android:layout_gravity="center">

原match_parent设置会让该布局占据父布局剩余全部高度,导致条目高度异常,可能挤压其他内容或让条目超出屏幕范围。

3. 验证图片资源(可选)

确保R.drawable.house、R.drawable.alle等图片资源确实存在于项目drawable目录下。如果资源缺失,ImageView会显示空白,但不会抛出异常,也可能导致条目看起来为空。

其他代码确认

你的Activity和Adapter代码逻辑无问题:

  • 数据添加后调用notifyDataSetChanged(),能正确通知Adapter更新
  • getItemCount()返回正确数量
  • ViewHolder绑定逻辑正确

修改布局后,RecyclerView即可正常显示3条测试数据。


内容的提问来源于stack exchange,提问作者Captai-N

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 18:05:17