绑定适配器无法正常工作 [英] Binding Adapter not working properly

查看:228
本文介绍了绑定适配器无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难使 @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"/>

这不行。命名空间应用程序未绑定。那我什么都没有我尝试以下
https:// medium.com/google-developers/android-data-binding-custom-setters-55a25a7aea47#6ygaiwooh
,看看他们如何设置bindingAdapter。但是有一些我错过了

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

推荐答案

我建议使用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"

这篇关于绑定适配器无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆