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

RecyclerView无数据显示求助:多次实现仍遇加载空白问题

问题排查与修复

嘿,我一眼就发现了导致RecyclerView空白的核心问题!你在自定义Adapter的loadTrailerList方法里犯了一个极易忽略的逻辑错误:

public void loadTrailerList(List<Trailer> trailers) {
    this.trailerList.clear();
    if (trailers != null) {
        trailers.addAll(trailers); // 这里是错误根源!
    }
    notifyDataSetChanged();
}

你现在是把参数trailers自己添加到自己里面,完全没有把数据存入Adapter的成员变量trailerList中!这就导致Adapter的数据源始终是空的,RecyclerView自然无法显示任何内容。

修正后的代码:

public void loadTrailerList(List<Trailer> trailers) {
    this.trailerList.clear();
    if (trailers != null) {
        this.trailerList.addAll(trailers); // 改为向成员变量trailerList添加数据
    }
    notifyDataSetChanged();
}

额外优化建议:

  1. 可以考虑使用DiffUtil替代直接调用notifyDataSetChanged(),这能提升RecyclerView的局部更新效率,属于修复后的优化项。
  2. 调整列表项布局的根容器宽度:当前trailer_list_item.xml的根LinearLayout宽度是wrap_content,如果标题文本较短,可能会显示不明显,建议改成match_parent让内容充满一行:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/text_view_trailer_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play Trailer" />
</LinearLayout>

这个错误属于编码时的小疏忽,修正后你的RecyclerView应该就能正常加载并显示数据啦!

内容的提问来源于stack exchange,提问作者Indiana

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 03:46:12