1. 程式人生 > 其它 >Jetpack dataBinding(三)上手指南 RecyclerView使用DataBinding

Jetpack dataBinding(三)上手指南 RecyclerView使用DataBinding

技術標籤:jetpack

RecyclerView中使用DataBinding

下面我們看下在RecycerView中的資料繫結如何使用,整體效果如圖:

1、在data標籤中定義variable

    <data>

        <variable
            name="adapter"
            type="androidx.recyclerview.widget.RecyclerView.Adapter" />

        <variable
            name="info"
            type="com.cniao5.demo.ItemBean" />


    </data>

2、在佈局檔案中引用

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:adapter="@{adapter}"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            tools:itemCount="5"
            tools:listitem="@layout/item_rv" />

在XML中定義了adapter、layoutManger、itemCount的數量、以及item的佈局檔案引用。

3、recyclerView內部的item佈局檔案也是dataBinding的形式

新增layout、data屬性

<layout xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <import type="com.cniao5.demo.BdTool" />

        <import type="com.cniao5.demo.BdToolKt" />

        <variable
            name="info"
            type="com.cniao5.demo.ItemBean" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp"
        android:background="@android:color/darker_gray">

        <TextView
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:text="@{BdToolKt.getTitle(info.text)}"
            android:textColor="@{info.type}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

可以看到在佈局檔案中使用了dataBinding的寫法來為item新增資料,並且使用到了自定義的一個類叫BdTool.kt,因此在開始需要使用import引入進來

<import type="com.cniao5.demo.BdTool" />
android:text="@{BdToolKt.getTitle(info.text)}"
android:textColor="@{info.type}"

4、adapter的寫法沒有什麼區別

class BdAdapter : RecyclerView.Adapter<BdAdapter.ItVH>() {

    private val mList = mutableListOf<ItemBean>()

    init {
        for (i in 0..5) {
            mList.add(ItemBean(i, "艾特木 $i"))
        }
    }

    class ItVH(private val binding: ItemRvBinding) : RecyclerView.ViewHolder(binding.root) {

        fun bind(bean: ItemBean) {
            binding.info = bean
            binding.executePendingBindings()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItVH {
        return ItVH(ItemRvBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun getItemCount() = mList.size

    override fun onBindViewHolder(holder: ItVH, position: Int) {
        holder.bind(mList[position])
    }
}

data class ItemBean(val type: Int, val text: String)

5、在activity中的使用

            adapter = BdAdapter()
            info = ItemBean(0, "include 的item")

完整專案程式碼