DataBinding学习笔记(Kotlin中 recycleview item建立双向绑定)

DataBinding属于jetpack中的一部分
在数据更新后的UI更新上有很大的帮助,大大提升代码效率
接下来就一起来入门学习DataBinding体会它的魅力!!

build.gradle中的配置(module)

    //module的build.gradle
    android {
        ....
        dataBinding {
               enabled = true
        }
    }

recycleview的item类

这边用到了Observable就可以观察数据的变化,以便通知进行UI更新

class Cartoon(cover: String, name: String, tags: String, decription: String){

    var cover : ObservableField<String> = ObservableField()
    var name : ObservableField<String> = ObservableField()
    var tags : ObservableField<String> = ObservableField()
    var decription : ObservableField<String> = ObservableField()
    init {
        this.cover.set(cover)
        this.name.set(name)
        this.tags.set(tags)
        this.decription.set(decription)
    }
}

item_layout.xml文件

data标签里是名称和类型,在 @{…} 中就可以获取对应的属性

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

    <data>
        <variable
            name="cartoon"
            type="com.example.youyaoqilite.data.Cartoon" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/item_cover"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{cartoon.cover}"/>
        <TextView
            android:id="@+id/cartoon_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/item_cover"
            android:layout_alignParentTop="true"
            android:text="@{cartoon.name}"/>
        <TextView
            android:id="@+id/cartoon_tags"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/item_cover"
            android:layout_below="@id/cartoon_name"
            android:text="@{cartoon.tags}"/>
        <TextView
            android:id="@+id/cartoon_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/item_cover"
            android:layout_below="@id/cartoon_tags"
            android:text="@{cartoon.decription}"/>
    </RelativeLayout>
</layout>

recycleview adapter类

设置适配器,我就按格式写(真小白实锤Fighting!!)

class RecyclerViewAdapter(private var cartoonList: MutableList<Cartoon>) : RecyclerView.Adapter<RecyclerViewAdapter.TestViewHolder>() {

    override fun getItemCount(): Int  = cartoonList.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestViewHolder {
        val binding = DataBindingUtil.inflate<ItemCartoonLayoutBinding>(
            LayoutInflater.from(parent.context),
            R.layout.item_cartoon_layout,
            parent,
            false
        )
        return TestViewHolder(binding)    }

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

    class TestViewHolder(private val binding: ItemCartoonLayoutBinding)
        : RecyclerView.ViewHolder(binding.root) {
        fun bind(data: Cartoon) {
            //方法一:
//            binding.setVariable(BR.user,data)
            //方法二:
            binding.cartoon = data
            binding.executePendingBindings()
        }
    }
}

Activity或是Fragment

//定义成员变量
val cartoonList = mutableListOf<Cartoon>()
//onCreate中初始化
val recyclerView : RecyclerView = findViewById(R.id.ranklist_recycleview)
        val layoutManager = LinearLayoutManager(this)
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = RecyclerViewAdapter(cartoonList)
        initCartoons()

然后就可以对数据进行修改体验效果,亲测有效
参考学习自https://blog.csdn.net/jameskaron/article/details/84916895
冲冲冲~~~~

你可能感兴趣的:(DataBinding学习笔记(Kotlin中 recycleview item建立双向绑定))