Recyclerview视图未填充



我是Kotlin的新手,正在学习如何创建简单的回收视图应用程序。我的代码应该在垂直堆叠的单元格中列出整数1..10。但是,它只列出了第一项。我查阅了几篇教程,并多次复习了我的代码(在长时间休息后(,但我看不出我的代码有任何错误。

今天早些时候,我有了打印日志报表的好主意。通过检查它们,我注意到我的onBindViewHolder函数只被调用过一次。我犯了什么错误?

这是我的日志输出:

D/QuoteAdapter: value is: 1
D/QuoteAdapter: index is: 0
D/QuoteAdapter: Size is: 10

我的活动:

class MainActivity : AppCompatActivity() {
lateinit var mRecyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mRecyclerView = findViewById(R.id.recyclerView)
mRecyclerView.layoutManager = LinearLayoutManager(this)
mRecyclerView.adapter = QuoteAdapter()
//mRecyclerView.setHasFixedSize(true)
}
}

我的适配器:

class QuoteAdapter : RecyclerView.Adapter<QuoteViewHolder>() {
private val listOfInts = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
private val TAG = "QuoteAdapter"
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QuoteViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.recyclerview_item_row, parent, false)
return QuoteViewHolder(view)
}
override fun getItemCount(): Int {
Log.d(TAG, "Size is: ${listOfInts.size.toString()}")
return listOfInts.size
}
override fun onBindViewHolder(holder: QuoteViewHolder, position: Int) {
val item = listOfInts[position]
Log.d(TAG, "value is: ${item.toString()}")
Log.d(TAG, "index is: ${position.toString()}")
holder.listTitle.text = item.toString()
}
}

我的看门人:

class QuoteViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val listTitle = itemView.findViewById(R.id.itemString) as TextView
}

我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/itemString"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

我的主要布局如下:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

在您的"我的布局";试试这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/itemString"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

注意LinearLayoutlayout_height已更改为wrap_content

也怀疑您是否需要ViewHolder的项目xml上的android:orientation="vertical",除非您将来只添加1个以上的TextView

正如Zain所说,您可以在布局文件中单独使用TextView,这也可以解决问题(只要它的高度是wrap_content!(

实际上,Android中包含了一些类型的android.R.layout.,你会看到一些东西,比如simple_list_item_1,它只是一个样式的TextView(你可以ctrl+单击引用或其他任何东西来查看文件(。如果你只是想做一件快速的事情,那就太好了!

android.R.layout.simple_list_item_1TextView的ID是@android:id/text1——注意android前缀,因为它是android资源的一部分,而不是您的应用程序的。这意味着您必须以与布局相同的方式引用ID,前面有androidandroid.R.id.text1

您可以在列表项布局中去掉LinearLayout,只保留TextView

因此,将您的列表项布局替换为:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/itemString"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

最新更新