如何解决无法使用数据绑定设置图像视图的背景
我试图在我的回收视图中设置图像视图的背景,但它没有显示任何图像的空白空间,当我使用占位符图像时,它显示占位符图像但不显示我想在图像视图中显示的图像 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")
}
}
解决方法
@BindingAdapter("imageName")
fun setImageFromURls(view: @BindingAdapter("imageName")
fun setImageFromURls(view: ShapeableImageView,fileName: String) {
Glide.with(view.context)
.load(fileName)
.into(view)
}
使用 ShapeableImageView 代替 ImageView 为我解决了这个问题
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。