如何在 RecyclerView 列表项中包含多个 TextView

How can I have multiple TextViews inside a RecyclerView list item

我知道 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>

您应该更改 ViewHolder 实现,因为它应该将您的父级 View(来自 xml 文件)作为参数而不是 TextViewTextViews 是子项,可以在您的 ViewHolder:

中定义为成员变量
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>() {

    class ScannedDevicesListViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
        val tv1 = view.TextView1
        val tv2 = view.TextView2
        val tv3 = view.TextView3
    }

    // Create new views (invoked by the layout manager)
    override fun onCreateViewHolder(parent: ViewGroup,
                                viewType: Int): 
    ScannedDevicesListAdapter.ScannedDevicesListViewHolder {
        // create a new view
        val linearLayout = LayoutInflater.from(parent.context)
        .inflate(R.layout.scanned_devices_list_item, parent, false) as View
       // set the view's size, margins, paddings and layout parameters
       return ScannedDevicesListViewHolder(linearLayout)
    }

    override fun onBindViewHolder(holder: ScannedDevicesListViewHolder, position: Int) {
        holder.tv1.text = myDataset[position]
        // set your other views here
    }

    // Return the size of your dataset (invoked by the layout manager)
    override fun getItemCount() = myDataset.size
}

你可以试试这个....

    class sel_cat_adapter(ctx: Context, list: MutableList<Any>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private var ctx: Context = ctx
    private var list: MutableList<Any> = list
    private var ITEM = 0
    private var Ads = 1
    var ITEM_PER_Ad = 5

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {


        when (viewType) {
            ITEM -> {
                var inflater =
                    LayoutInflater.from(ctx).inflate(R.layout.customlayoutfor_sel_cat, null, false)
                return viewholderclass(inflater)
            }
            Ads -> {
                var inflater = LayoutInflater.from(ctx).inflate(R.layout.ads_banner, null, false)
                return AdsViewHolder(inflater)
            }
            else -> {
                var inflater = LayoutInflater.from(ctx).inflate(R.layout.ads_banner, null, false)
                return AdsViewHolder(inflater)
            }
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        var viewtype = getItemViewType(position)
        when (viewtype) {
            ITEM -> {
                var myholder = holder as viewholderclass
            }
            Ads -> {
                var myholder = holder as AdsViewHolder
            }
            else -> {

            }
        }
        holder.itemView.setOnClickListener {
            if (viewtype == ITEM) {
               Toast.makeText(ctx, "Item", Toast.LENGTH_LONG).show()
            } else {
                Toast.makeText(ctx, "Ads", Toast.LENGTH_LONG).show()
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (position % ITEM_PER_Ad == 0) {
            if (list[position] is AdView) {
                Ads
            } else {
                ITEM
            }
        } else {
            ITEM
        }
    }

    class AdsViewHolder(itemview: View) : RecyclerView.ViewHolder(itemview){

    }


    class viewholderclass(itemview: View) : RecyclerView.ViewHolder(itemview) {


    }
}