[英]How to databind imageview with drawable in viewmodel?
Binding Adapter: 绑定适配器:
@BindingAdapter("src")
fun loadImage(imageView: ImageView, src: Any) {
if (src is Int || src is String || src is Drawable) {
Glide.with(imageView.context).load(src).into(imageView)
}
}
Imageview in xml: xml中的Imageview:
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="200dp"
android:layout_height="300dp"
app:src="@{model.image}" />
View model: 查看模型:
val image = MutableLiveData<Drawable>()
init {
// image.value = How to initialize this?
}
Scenario: 场景:
I want to bind imageview with drawable received from server. 我想将imageview与从服务器收到的drawable绑定。 I want to set a placeholder drawable till the image is obtained from server.
我想设置一个可绘制的占位符,直到从服务器获取图像。
Error for the above code: 上面的代码错误:
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter src
java.lang.IllegalArgumentException:指定为非null的参数为null:方法kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数src
Note: 1. Please provide a solution which follows MVVM. 注意: 1.请提供遵循MVVM的解决方案。 (No context in viewmodel)
(视图模型中没有上下文)
You can set the placeholder in Glide, instead of handling that in your ViewModel
. 您可以在Glide中设置占位符,而不是在
ViewModel
进行处理。 Just the following line should serve your purpose. 以下几行应该可以满足您的目的。
Glide.with(imageView.context).load(src).placeholder(R.drawable.placeholder).into(imageView)
See the documentation here . 请参阅此处的文档 。
Extending @Reaz Murshed's idea and after refering Android Binding Adapter Docs , I created a binding adapter for all imageview's. 扩展@Reaz Murshed的想法,并在引用Android Binding Adapter Docs之后 ,为所有imageview的对象创建了一个绑定适配器。 (All imageview's need not have the same placeholder)
(所有imageview不必具有相同的占位符)
Binding Adapter: 绑定适配器:
@BindingAdapter(value = ["src", "placeholderImage"], requireAll = false)
fun loadImage(imageView: ImageView, src: Int?, placeholderImage: Drawable?) {
if (placeholderImage != null) {
Glide.with(imageView.context).load(src).placeholder(placeholderImage).into(imageView)
} else {
Glide.with(imageView.context).load(src).into(imageView)
}
}
@BindingAdapter(value = ["src", "placeholderImage"], requireAll = false)
fun loadImage(imageView: ImageView, src: String?, placeholderImage: Drawable?) {
if (placeholderImage != null) {
Glide.with(imageView.context).load(src).placeholder(placeholderImage).into(imageView)
} else {
Glide.with(imageView.context).load(src).into(imageView)
}
}
@BindingAdapter(value = ["src", "placeholderImage"], requireAll = false)
fun loadImage(imageView: ImageView, src: Drawable?, placeholderImage: Drawable?) {
if (placeholderImage != null) {
Glide.with(imageView.context).load(src).placeholder(placeholderImage).into(imageView)
} else {
Glide.with(imageView.context).load(src).into(imageView)
}
}
XML: XML:
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="200dp"
android:layout_height="300dp"
app:placeholderImage="@{@drawable/kids_ethnic_1}"
app:src="@{model.image}" />
If needed we can add other methods to make placeholder of Int
or other datatype as required. 如果需要,我们可以根据需要添加其他方法以使
Int
或其他数据类型占位符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.