【发布时间】: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