美文网首页
Mvvm+DataBinding如何在RecycleView 的

Mvvm+DataBinding如何在RecycleView 的

作者: ae12 | 来源:发表于2022-12-21 17:19 被阅读0次

实战经验:
1.app builder 添加Glide 依赖:
implementation 'com.github.bumptech.glide:glide:4.9.0'
step 2:
自定义object BindingAdapter 实现图片加载方法loadImage 如下:
object BindingAdapter {
/**
* A Binding Adapter that is called whenever the value of the attribute app:popularityIcon
* changes. Receives a popularity level that determines the icon and tint color to use.
*/

  @BindingAdapter(value=["imageUrl", "placeHolder", "error"], requireAll = false)
   @JvmStatic
   fun loadImage(
       imageView: ImageView,
       url: String?,
       holderDrawable: Drawable?,
       errorDrawable: Drawable?
   ) {
       Glide.with(imageView.context)
           .load(url)
           .placeholder(holderDrawable)
           .error(errorDrawable)
           .into(imageView)
   }
}

step3:
在item 布局文件里使用如下:

<data>
       <variable
           name="data"
           type="xxx.EvidenceOriginModel" />
   </data>
<ImageView
           android:id="@+id/iv_material"
           android:layout_width="@dimen/px_220"
           android:layout_height="@dimen/px_156"
           app:layout_constraintTop_toTopOf="parent"
           app:layout_constraintStart_toStartOf="parent"
           app:imageUrl="@{data.thumbUrl}"
           android:background="@drawable/bg_e4e9f3_corner_4" />

完结,
看着简单,但是踩了点坑,如下图所示


企业微信截图_1671700654415.png

相关文章

网友评论

      本文标题:Mvvm+DataBinding如何在RecycleView 的

      本文链接:https://www.haomeiwen.com/subject/paumqdtx.html