【问题标题】:RecyclerView is Not Displaying Adapter's DataRecyclerView 不显示适配器的数据
【发布时间】:2021-12-07 09:54:15
【问题描述】:

我的RecyclerView 显示为空CardView。我看不出我哪里出错了。 Android Studio 在编译时不会失败,应用程序在运行时也不会失败。任何帮助将不胜感激。

这是我的数据类

data class Notes(
    var title: String,
    var description: String
)

这是我的适配器

class MyNotesAdapter(private val notesList: MutableList<Notes>) : RecyclerView.Adapter<MyNotesAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var textViewTitle: TextView = view.findViewById(R.id.textViewTitle)
        var textViewDescr: TextView = view.findViewById(R.id.textViewDescr)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        var view = LayoutInflater.from(parent.context).inflate(R.layout.my_notes_adapter_layout,
            parent, false) 
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.textViewTitle.text = notesList[position].title
        holder.textViewDescr.text = notesList[position].description
    }

    override fun getItemCount(): Int = notesList.size
}

这是我的适配器布局

<?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="match_parent"
        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_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />
        <TextView
            android:id="@+id/textViewDescr"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            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" />
    </androidx.constraintlayout.widget.ConstraintLayout>

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

这就是我在主要活动中设置 RecyclerView 的方式

private fun setupRecyclerView() {
    val myNotesAdapter: MyNotesAdapter = MyNotesAdapter(notesList)
    val linearLayoutManager: LinearLayoutManager = LinearLayoutManager(this)
    linearLayoutManager.orientation = RecyclerView.VERTICAL

    recyclerViewNotes = findViewById(R.id.recyclerViewNotes)
    recyclerViewNotes.layoutManager = linearLayoutManager
    recyclerViewNotes.adapter = myNotesAdapter
}

我的 RecyclerView 函数在这里被调用

private fun setupDialogBox() {
        var view = LayoutInflater.from(this).inflate(R.layout.add_notes_dialog_layout, null)
        var dialog = AlertDialog.Builder(this)
            .setView(view)
            .setCancelable(false)
            .create()
        dialog.show()

        buttonSubmit = view.findViewById(R.id.buttonSubmit)
        editTextTitle = view.findViewById(R.id.editTextTitle)
        editTextDescr = view.findViewById(R.id.editTextDescr)

        buttonSubmit.setOnClickListener {
            notes = Notes(editTextTitle.text.toString(), editTextDescr.text.toString())
            notesList.add(notes)
            setupRecyclerView()
            dialog.hide()
        }
    }

我的对话框在主要活动onCreate()中被调用

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_my_notes)
    ......

    fabButton.setOnClickListener {
        setupDialogBox()
    }
}

【问题讨论】:

  • 可能是您的ConstraintLayout 上的match_parent。它的父级设置为wrap_content,因此那里存在冲突。在ConstraintLayout 的高度上尝试wrap_content
  • 如果@StuStirling 说的是真的(可能是,我有时会遇到这个问题),你应该仍然可以滚动项目,它们只是占据了整个屏幕。
  • 查看noteList的大小,应该是空的。这就是它显示列表项的原因
  • 托管对话框的片段或活动应该调用setupRecyclerView()

标签: android kotlin android-recyclerview android-constraintlayout


【解决方案1】:
  1. 检查noteList的大小,应该是空的。这就是它显示列表项的原因

  2. 托管对话框的 Fragment 或 Activity 应该调用 setupRecyclerView()

  3. 你在 setupDialogBox()中多次调用setupRecyclerView()

  4. 如果这些都不起作用,那么给 CardView 一个高度,不要使用wrap_content,我只有在使用 LinearLayout 时才使用wrap_content

【讨论】:

  • 为什么我的适配器应该是空的。我必须传递充满数据的 notesList,以便适配器将数据馈送到回收站视图中,这就是我的做法 val myNotesAdapter: MyNotesAdapter = MyNotesAdapter(notesList) ..... 我稍后会尝试你的建议。我希望它有效
【解决方案2】:
<TextView
    android:id="@+id/textViewTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TITLE HERE"
    ....
    app:layout_constraintTop_toBottomOf="parent" />

问题在于ConstraintLayout,因为顶部TextViewapp:layout_constraintTop_toBottomOf="parent" 设置为ConstraintLayout 的底部。

这包括ConstraintsLayout 的高度是从match_parent 计算出来的,即它等于父级(因此,它占用了项目布局的全部高度),因此不会显示TextViews在列表项或屏幕上。

同样由于 CardView 的高度是 wrap_content,那么 ConstraintLayout 将倾向于采用该 ViewGroup 的最小高度。

要解决此问题,您需要替换 textViewTitle 的此约束:

`app:layout_constraintTop_toBottomOf="parent"`

与:

`app:layout_constraintTop_toTopOf="parent"`

【讨论】:

    猜你喜欢
    • 2018-05-29
    • 2020-07-02
    • 2016-02-15
    • 2017-02-24
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    相关资源
    最近更新 更多