问题描述
我很难使 @BindingAdapter
在我的项目中工作。
I have a hard time making @BindingAdapter
to work in my project.
@BindingAdapter("imageUrl")
public static void setImageUrl(ImageView imageView, String url) {
Log.d("TEST","URL: " + url);
}
以上代码显示了如何在ViewModel中实现。没有什么特别的。
Above code shows how it is implemented in my ViewModel. Nothing special.
<ImageView
android:id="@+id/image_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:layout_below="@id/profile_container"
app:imageUrl="@{item.imageUrl}"
tools:src="@drawable/placeholder_image"/>
这不行。命名空间应用程序未绑定。那我什么都没有我尝试以下
,看看他们如何设置bindingAdapter。但是有一些我错过了
This does not work. namespace app is unbound. So what am i missing. I tried followinghttps://medium.com/google-developers/android-data-binding-custom-setters-55a25a7aea47#.6ygaiwoohand see how they set bindingAdapter. But there is something i have missed
推荐答案
我建议使用bind命名空间来绑定属性并且为适配器参数和布局属性使用相同的名称。
I suggest to use "bind" namespace for bindable attributes and use same names for adapter parameter and layout attribute.
@BindingAdapter("bind:imageUrl")
public static void setImageUrl(ImageView imageView, String imageUrl) {
Log.d("TEST","URL: " + imageUrl);
}
布局:
Layout:
<ImageView
android:id="@+id/image_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:layout_below="@id/profile_container"
bind:imageUrl="@{item.imageUrl}"
tools:src="@drawable/placeholder_image"/>
其中namespace app被替换为bind
where namespace "app" was replaced to "bind"
这篇关于绑定适配器无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!