BaseQuickAdapter 使用數據綁定DataBinding

目前初始使用的優點1是

在僅僅數據展示的情況下 無需寫id 只需要在需要設置點擊事件加上id即可

例如

<?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"
   xmlns:tools="http://schemas.android.com/tools">

   <data>

       <variable
           name="item"
           type="me.gavin.adapter.base.Story" />

   </data>

   <LinearLayout
       android:id="@+id/item"
       android:layout_width="match_parent"
       android:layout_height="82dp"
       android:clickable="true"
       android:foreground="?attr/selectableItemBackground"
       android:gravity="center_vertical"
       android:paddingLeft="16dp"
       android:paddingRight="16dp">

       <ImageView
           android:layout_width="50dp"
           android:layout_height="50dp"
           android:scaleType="centerCrop"
           app:imageUrl="@{item.image}"
           tools:ignore="contentDescription"
           tools:src="@mipmap/ic_launcher" />

       <TextView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_marginStart="12dp"
           android:layout_marginLeft="12dp"
           android:lineSpacingMultiplier="1.2"
           android:maxLines="2"
           android:text="@{item.title}"
           android:textColor="#99000000"
           android:textSize="14sp" />
 <TextView
                android:id="@+id/mPrintMsgListPrint"
                android:layout_width="wrap_content"
                android:text="點擊事件"
                android:textColor="#FFFFFFFF"
                android:textSize="20sp" />
   </LinearLayout>

</layout>
kotlin 適配器
class PrintMsgListAdapter(layout: Int) : BaseDataBindRecyclerAdapter<PartsPrintListResult.Data.PartsStorageListDto>(layout) {
    override fun convert(helper: DataBindingAdapter?, entity: PartsPrintListResult.Data.PartsStorageListDto) {
        super.convert(helper, entity)
        helper?.apply {
            helper.binding?.setVariable(item, entity)
            helper?.addOnClickListener(R.id.mPrintMsgListPrint)
            binding?.executePendingBindings()
        }


    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章