【发布时间】:2024-04-25 15:45:02
【问题描述】:
我知道 RecyclerView 列表很灵活,并且每个项目都可以有多个视图。我找不到任何可以帮助我实现它的好的示例代码。
我正在尝试实现一个简单的 RecyclerView 列表,其中每个列表项作为我创建的自定义 .xml 布局。为了使 RecyclerView.Adapter 能够通过自定义布局文件中的 ID 访问每个不同的视图并能够更新每个值,必须做些什么?
我基本上从Android Docs复制了以下代码。
//ScannedDevicesListAdapter.kt
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class ScannedDevicesListAdapter(private val myDataset: Array<String>) :
RecyclerView.Adapter<ScannedDevicesListAdapter.ScannedDevicesListViewHolder>() {
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder.
// Each data item is just a string in this case that is shown in a TextView.
class ScannedDevicesListViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
// Create new views (invoked by the layout manager)
override fun onCreateViewHolder(parent: ViewGroup,
viewType: Int): ScannedDevicesListAdapter.ScannedDevicesListViewHolder {
// create a new view
val textView = LayoutInflater.from(parent.context)
.inflate(R.layout.scanned_devices_list_item, parent, false) as TextView
// set the view's size, margins, paddings and layout parameters
return ScannedDevicesListViewHolder(textView)
}
// Replace the contents of a view (invoked by the layout manager)
override fun onBindViewHolder(holder: ScannedDevicesListViewHolder, position: Int) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.textView.text = myDataset[position]
}
// Return the size of your dataset (invoked by the layout manager)
override fun getItemCount() = myDataset.size
}
这是我的自定义视图的 .xml 文件:
Screenshot of the custom list item
//scanned_devices_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:padding="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:text="Field 1" />
<TextView
android:id="@+id/TextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:text="Field 2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/TextView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:text="Field 3" />
</LinearLayout>
</LinearLayout>
【问题讨论】:
-
R.layout.scanned_devices_list_item似乎是LinearLayout而不是TextView。 -
回收器视图期望控件具有分配给它的标签。 id可以回收。
标签: android kotlin android-recyclerview android-custom-view