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

Android开发RecyclerView不显示Adapter数据 仅展示空白CardView求助

问题原因及修复方案

1. 适配器布局约束错误

当前item布局中textViewTitle的约束配置错误:app:layout_constraintTop_toBottomOf="parent"表示将TextView的顶部对齐到父布局的底部,直接超出了ConstraintLayout的可视区域,所以两个TextView都不可见,仅显示空白Card。同时布局的高度配置也存在异常,修正后的完整布局如下:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="16dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp">
        <TextView
            android:id="@+id/textViewTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TITLE HERE"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:textColor="@color/black"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />
        <TextView
            android:id="@+id/textViewDescr"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="DESCR HERE"
            android:textColor="@color/black"
            app:layout_constraintTop_toBottomOf="@id/textViewTitle"
            app:layout_constraintStart_toStartOf="@id/textViewTitle"
            app:layout_constraintEnd_toEndOf="@id/textViewTitle"
            app:layout_constraintBottom_toBottomOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.card.MaterialCardView>

2. 列表初始化逻辑错误

当前写法每次提交笔记都重新初始化整个RecyclerView,不符合使用规范,且第一次打开页面时RecyclerView没有初始化,只有第一次提交数据才会初始化。修正逻辑如下:

  1. 先在Activity中声明全局变量
private lateinit var myNotesAdapter: MyNotesAdapter
private val notesList = mutableListOf<Notes>()
  1. 在onCreate中初始化RecyclerView,不要等到添加数据才初始化
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_my_notes)
    // 页面创建时直接初始化列表
    setupRecyclerView()
    ......

    fabButton.setOnClickListener {
        setupDialogBox()
    }
}
  1. 改造setupRecyclerView方法,仅做初始化操作
private fun setupRecyclerView() {
    myNotesAdapter = MyNotesAdapter(notesList)
    val linearLayoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
    recyclerViewNotes = findViewById(R.id.recyclerViewNotes)
    recyclerViewNotes.layoutManager = linearLayoutManager
    recyclerViewNotes.adapter = myNotesAdapter
}
  1. 修改提交按钮逻辑,添加数据后直接刷新适配器即可,不要重复初始化列表
buttonSubmit.setOnClickListener {
    notes = Notes(editTextTitle.text.toString(), editTextDescr.text.toString())
    notesList.add(notes)
    // 仅刷新新增的条目即可
    myNotesAdapter.notifyItemInserted(notesList.size - 1)
    dialog.hide()
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 02:09:00