在 Adapter Kotlin 中绑定数据

Binding data inside an Adapter Kotlin

我创建了下面的适配器,它在回收器视图中显示了两种不同的数据模型。

但是,我不确定如何在 ViewHolders 中编写的绑定函数中进行绑定。我有两个单独的 xml 文件,我想在调用此“绑定”函数时绑定它们,但如何设置数据?

我的代码如下:

class HomeAdapter(
    private val context: Context
    ) :
    RecyclerView.Adapter<HomeAdapter.BaseViewHolder<*>>() {
    private var homeList: List<Any> = emptyList()

    companion object {
        private const val TYPE_VISIT = 0
        private const val TYPE_WASH = 1

    }

    abstract class BaseViewHolder<T>(itemView: View) : RecyclerView.ViewHolder(itemView) {
        abstract fun bind(item: T)
    }

    inner class VisitViewHolder(itemView: View) : BaseViewHolder<HomeVisitLabel>(itemView) {

        override fun bind(item: HomeVisitLabel) {
            //Do your view assignment here from the data model
            
        }
    }

    inner class WashViewHolder(itemView: View) : BaseViewHolder<HomeWashLabel>(itemView) {

        override fun bind(item: HomeWashLabel) {
            //Do your view assignment here from the data model
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<*> {
        return when (viewType) {
            TYPE_VISIT -> {
                val view = LayoutInflater.from(context)
                    .inflate(R.layout.reward_label_visit_card, parent, false)
                VisitViewHolder(view)
            }
            TYPE_WASH -> {
                val view = LayoutInflater.from(context)
                    .inflate(R.layout.reward_label_wash_card, parent, false)
                WashViewHolder(view)
            }

            else -> throw IllegalArgumentException("Invalid view type")
        }
    }

    override fun onBindViewHolder(holder: BaseViewHolder<*>, position: Int) {
        val element = homeList[position]
        when (holder) {
            is VisitViewHolder -> holder.bind(element as HomeVisitLabel)
            is WashViewHolder -> holder.bind(element as HomeWashLabel)
            else -> throw IllegalArgumentException()
        }
    }

    override fun getItemViewType(position: Int): Int {
        val comparable = homeList[position]
        return when (comparable) {
            is HomeVisitLabel -> TYPE_VISIT
            is HomeWashLabel -> TYPE_WASH
            else -> throw IllegalArgumentException("Invalid type of data " + position)
        }
    }

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


}

两个 XML 文件之一

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="VisitLabel"
            type="com.modelz.HomeVisitLabel" />

    </data>

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="5dp"
        android:layout_margin="5dp"
        app:cardBackgroundColor="@color/colorPrimary">



            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginLeft="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="18sp"
                        android:text="Visit:"
                        android:gravity="left"
                        android:layout_weight="1"
                        android:layout_marginTop="5dp"
                        android:textStyle="bold"/>
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="18sp"
                        android:text="@{VisitLabel.Name}"
                        android:gravity="left"
                        android:layout_weight="1"
                        android:layout_marginTop="5dp"
                        android:textStyle="bold"/>

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Redeem"
                        android:visibility="invisible"/>

                </LinearLayout>


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:layout_weight="0.5"
                    android:gravity="left"
                    android:text="@{VisitLabel.descript}"
                    android:textSize="16dp" />


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:layout_weight="0.5"
                        android:gravity="left"
                        android:text="Progress:"
                        android:textSize="16dp" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:layout_weight="0.9"
                        android:text="@{VisitLabel.countUser}"
                        android:gravity="right"
                        android:textSize="16sp" />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:layout_weight="0.5"
                        android:text="@{VisitLabel.countSet}"
                        android:gravity="left"
                        android:textSize="16sp" />

                </LinearLayout>

            </LinearLayout>

    </androidx.cardview.widget.CardView>



</layout>

您需要完成两个视图持有者的绑定功能:

override fun bind(item: HomeVisitLabel) {
        // textView.text = item.name // example
    }

您应该能够在上面的函数中分配值,如示例所示,这应该像在 onBindViewHolder 中一样工作,根据数组中位置处的数据类型调用绑定函数,允许要绑定到不同 xml 布局文件的数据。

您必须完成两个查看器的绑定功能,例如将文本视图设置为项目数据字符串或为图像视图设置图像资源,对应于数据类型的正确 xml 布局文件。

如果您想使用数据绑定,您需要从 Binding class 扩展您的布局,如下所示:

val binding = RewardLabelVisitCardBinding.inflate(layoutInflater, parent, false)

其他布局也一样。 你可以得到 layoutInflator 使用:

val layoutInflater = LayoutInflater.from(parent.context)

然后在 bind 函数中,您需要使用 binding 变量来分配属性。喜欢,

binding.textView.text = item.name

为了更好的理解,下面给出整体代码:

class VisitViewHolder(private val binding: RewardLabelVisitCardBinding) : BaseViewHolder<HomeVisitLabel>(binding.root) {

    override fun bind(item: HomeVisitLabel) {
        binding.VisitLabel = item
        binding.executePendingBindings()
    }
}

onCreateViewHolder

中扩充布局后,只需将 binding 变量传递给 ViewHolder
return VisitViewHolder(binding)

希望这能回答您的问题!