【问题标题】:Not able to set background of imageview using databinding无法使用数据绑定设置图像视图的背景
【发布时间】:2021-04-05 16:53:00
【问题描述】:

我正在尝试在我的 recyclerview 中设置 imageview 的背景,但它没有显示任何内容,只有图像的空白空间,当我使用占位符图像时,它显示占位符图像,但不显示我想在 imageview 中显示的图像 URL

我的代码

@BindingAdapter("imageName")
fun setImageFromURls(view: ImageView, fileName: String) {
    Glide.with(view.context)
            .load(fileName)
            .into(view)
}

回收站视图

 <com.google.android.material.imageview.ShapeableImageView
            android:id="@+id/roundedImageView"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:adjustViewBounds="true"
            app:imageName="@{productitem.image}"
            android:scaleType="fitXY"
            app:shapeAppearanceOverlay="@style/roundimageview" />

产品

@Parcelize
data class Product(

        val image: String
        ):Parcelable

数据提供者

object DataProvider {

    val productList: MutableList<Product> = ArrayList()

    private fun addProduct(imageUri: String) {

        val item = Product(imageUri)
        productList.add(item)


    }
    init {
        addProduct("https://images.unsplash.com/photo-1598128558393-70ff21433be0?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1922&q=80")
        addProduct("https://images.unsplash.com/photo-1598128558393-70ff21433be0?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1922&q=80")
        addProduct("https://images.unsplash.com/photo-1581090700227-1e37b190418e?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1050&q=80")
        addProduct("https://images.unsplash.com/photo-1581092918056-0c4c3acd3789?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&auto=format&fit=crop&w=1050&q=80")
        addProduct("https://images.unsplash.com/photo-1473968512647-3e447244af8f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1950&q=80")

    }
}

【问题讨论】:

  • 我认为您的数据提供者存在问题,请您在setImageFromURls 函数中写入日志,以检查是否传递给绑定适配器的图像 url
  • 使用日志语句将这些 URL 传递给 setImageFromURls 函数
  • 好的,现在我们将尝试另一件事,将静态 URL 放入您的数据绑定函数中,以检查它是否可以使用像 Glide.with(view.context) .load("add URL here for test ") .into(view) 这样的静态 URL,如果可以,我们将检查其他情况
  • 谢谢,但我找到了解决方案,@bindviewholder 的参数应该是 ShapeableImageView 而不是 Imageview,现在可以正常工作

标签: android kotlin android-databinding


【解决方案1】:
@BindingAdapter("imageName")
fun setImageFromURls(view: @BindingAdapter("imageName")
fun setImageFromURls(view: ShapeableImageView, fileName: String) {
    Glide.with(view.context)
            .load(fileName)
            .into(view)
}

使用 ShapeableImageView 代替 ImageView 解决了我的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多