14

@BindingAdapter私は自分のプロジェクトで作業するのに苦労しています。

@BindingAdapter("imageUrl")
public static void setImageUrl(ImageView imageView, String url) {
    Log.d("TEST","URL: " + url);
}

上記のコードは、ViewModel での実装方法を示しています。特にない。

    <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 の設定方法を確認しました。しかし、私が見逃したものがあります

4

6 に答える 6

5

バインド可能な属性には「バインド」名前空間を使用し、アダプター パラメーターとレイアウト属性には同じ名前を使用することをお勧めします。

アダプタ:

@BindingAdapter("bind:imageUrl")
public static void setImageUrl(ImageView imageView, String imageUrl) {
     Log.d("TEST","URL: " + imageUrl);
}

レイアウト:

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

名前空間"app"は"bind"に置き換えられました。レイアウト ルートで:

 xmlns:bind="http://schemas.android.com/apk/res-auto"
于 2017-01-09T10:14:20.677 に答える