Binding Adapter not working properly

匿名 (未验证) 提交于 2019-12-03 00:54:02

问题:

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); } 

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"/> 

This does not work. namespace app is unbound. So what am i missing. I tried following https://medium.com/google-developers/android-data-binding-custom-setters-55a25a7aea47#.6ygaiwooh and see how they set bindingAdapter. But there is something i have missed

回答1:

I encountered the same problem, I missed to bind layout using:

DataBindingUtil.setContentView(activity, layoutResId); 


回答2:

Give it a try with

@BindingAdapter({"bind:imageUrl"}) 


回答3:

I suggest to use "bind" namespace for bindable attributes and use same names for adapter parameter and layout attribute.

Adapter:

@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"/> 

where namespace "app" was replaced to "bind"



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!