android - 如何使用数据类中的 Glide 绑定(bind) RecyclerView 图像

标签 android android-databinding android-glide

我正在尝试将回收器 View 与数据绑定(bind)类绑定(bind),该类与以下代码完美配合

@BindingAdapter("retailPrice")
fun TextView.setRetailPrice(item: VegetableDailyPriceCalender?){
    item?.let {
        text = item.retailPrice.toString()
    }
}

@BindingAdapter("ourPrice")
fun TextView.setOurPrice(item: VegetableDailyPriceCalender?){
    item?.let {
        text = item.ourPrice.toString()
    }
}

@BindingAdapter("itemName")
fun TextView.setItemName(item: VegetableDailyPriceCalender?){
item?.let {
    text = item.vegetable.name
    text
}
}

@BindingAdapter("itemImage")
fun ImageView.setSleepImage(item: VegetableDailyPriceCalender?) {
item?.let {
    setImageResource(R.drawable.ic_launcher_foreground)
}
}

现在我想使用 Glide 库转换最后一个绑定(bind)以从互联网加载动态图像Glide 需要如下代码

fun bindImage(imgView: ImageView, imgUrl: String?){
    imgUrl?.let {
        val imgUri = it.toUri().buildUpon().scheme("https").build()
        Glide.with(imgView.context)
            .load(imgUri)
            .apply(
                RequestOptions()
                    .placeholder(R.drawable.loading_animation)
                    .error(R.drawable.ic_broken_image))
            .into(imgView)
    }
}

任何建议都会非常有帮助。

更新... 我尝试了以下方法,但混淆了将什么传递给 with()into()

fun ImageView.setSleepImage(item: VegetableDailyPriceCalender?) {
    val imgUri = item.vegetable.photoUrl.toUri().buildUpon().scheme("https").build()
    item?.let {
        setImageResource(Glide.with(imgView.context)
            .load(imgUri)
            .apply(
                RequestOptions()
                    .placeholder(R.drawable.loading_animation)
                    .error(R.drawable.ic_broken_image))
            .into(imgView))
    }
}

最佳答案

创建 BindingAdapter 如下所示:

@BindingAdapter("imageUrl")
fun setImageUrl(imgView: ImageView, imgUrl: String?){

    imgUrl?.let {
        val imgUri = it.toUri().buildUpon().scheme("https").build()
        Glide.with(imgView.context)
            .load(imgUri)
            .apply(
                RequestOptions()
                    .placeholder(R.drawable.loading_animation)
                    .error(R.drawable.ic_broken_image))
            .into(imgView)
    }
}

并使用如下:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    imageUrl="@{viewModel.imageUrl}"/>

您还可以使用像您一样的扩展功能

@BindingAdapter("itemImage")
fun ImageView.setSleepImage(item: VegetableDailyPriceCalender?) {
    val imgUri = item.vegetable.photoUrl.toUri().buildUpon().scheme("https").build()
    item?.let {
        Glide.with(context)
            .load(imgUri)
            .apply(
                RequestOptions()
                    .placeholder(R.drawable.loading_animation)
                    .error(R.drawable.ic_broken_image))
            .into(this)
    }
}

关于android - 如何使用数据类中的 Glide 绑定(bind) RecyclerView 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59641643/

相关文章:

android - 为什么使用 ViewBinding 而不是 DataBinding?

Android 滑动加载位图

android - 添加 Room 编译器后找不到 GlideApp

android - 在 viewpager fragment 内的 recyclerview 项目与 viewpager fragment 之间共享元素转换

android - Android 数据绑定(bind)的 Kotlin 问题

java - 为什么以及何时将 @JvmStatic 与伴随对象一起使用?

Android数据绑定(bind)三元运算符错误

java - GridView 中使用 Glide 加载图像太慢

android - 在 Android 中创建较小的标签

android - 有没有办法告诉 Room 自定义 sqlite 函数?