[英]RecyclerView is Not Displaying Adapter's Data
我的RecyclerView
显示空CardView
。 我不明白我哪里出错了。 Android Studio 在编译 tile 时不会失败,应用程序在运行时也不会失败。 任何帮助将不胜感激。
这是我的数据类
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()
}
}
<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
因为顶部TextView
的app: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"`
检查noteList
的大小,它应该是空的。 这就是它显示列表项的原因
托管对话框的 Fragment 或 Activity 应该调用setupRecyclerView()
你一再呼吁setupRecyclerView()
在setupDialogBox()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.