首页 > 解决方案 > Imageview 的图像未在数据绑定中的 bindingadapter 中设置

问题描述

我正在尝试通过绑定适配器在图像视图中设置图像。我正在传递两个参数、一个图标和一个条件。我已登录以检查该函数是否被调用。该函数被调用,但图像视图未设置图像。

图像视图代码:

        <ImageView
        android:id="@+id/iv_remove"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginStart="16dp"
        isEntryField="@{group_field_wtih_values.is_entry_field}"
        icon="@{group_field_wtih_values.is_entry_field ? ResourceProvider.enabledRemoveBtn:ResourceProvider.disabledRemoveBtn}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

绑定适配器代码:

 @BindingAdapter({"icon","isEntryField"})
public static void setImageResource(ImageView imageView,Integer icon,boolean isEntryField){
    Log.e("setImageResources",String.valueOf(icon));
    if (!isEntryField)
        imageView.setImageDrawable(imageView.getContext().getResources().getDrawable(icon));
}

标签: androidandroid-databinding

解决方案


推荐阅读