Android 使用数据绑定加载图像

Android 使用数据绑定加载图像,android,android-databinding,Android,Android Databinding,我想使用Android数据绑定设置图像。我已经阅读了很多关于这方面的教程,但图像仍然没有出现 在我的模型中,我有以下方法: @BindingAdapter({"app:imageUrl"}) public static void loadImage(ImageView view, String url) { Log.i("III", "Image - " + url); Context context = view.getContext();

我想使用Android数据绑定设置图像。我已经阅读了很多关于这方面的教程,但图像仍然没有出现

在我的模型中,我有以下方法:

@BindingAdapter({"app:imageUrl"})
    public static void loadImage(ImageView view, String url) {

        Log.i("III", "Image - " + url);
        Context context = view.getContext();
        Glide.with(context).load(GlobalValues.img_start_url + url).into(view);
    }
@BindingAdapter(value = "imageDrawableRes")
public static void bindErrorImageView(ImageView imageView, int imageDrawableRes) {

    Glide.with(MyApplication.getContext())
        .load(imageDrawableRes)
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .centerCrop()
        .error(placeHolder)
        .placeholder(placeHolder)
        .into(imageView);

}
我还应该提到的是,“III”,“Image-”+url从来没有打印过,所以问题不在于Glide

这是我的xml:

 <data>

    <variable
        name="feeling"
        type="bg.web3.helpkarma.ViewModels.Feeling"/>

</data>

<ImageView
            android:layout_width="wrap_content"
            android:id="@+id/icon"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp"
            app:imageUrl="@{feeling.maleIcon}"/>

Android数据绑定
@BindingAdapter
仅支持Android命名空间或不支持。因此,将定义替换为

@BindingAdapter("imageUrl")
public static void loadImage(ImageView view, String url) {
    [...]
}
此外,如果
feeling.maleIcon
为空,则永远不会调用
loadImage()

以下是步骤: 1.在模型类中的注释@BindAdapter中声明此方法(“{bind:应该是我们获取imageurl的同一变量”})

2.转到适配器的xml文件并添加app:imageUrl=“@{user.imageUrl}”。在这一行中,属性app:imageurl和user.imageurl都应该是同一个参数,在这个参数中,我们将获得bean类中图像的url。我们也在第一步中定义了相同的内容

 <ImageView
            android:id="@+id/restaurantIV"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:scaleType="fitXY"
            app:image="@{user.image}"
            tools:ignore="ContentDescription"/>
行中:

fun loadImage(layout: ConstraintLayout, url:String)
添加到url的类型。结果如下:

fun loadImage(layout: ConstraintLayout, url:String?)

在我的例子中,我将布局作为参数,而不是图像视图。这应该行得通。

我使用了上面的一个答案,但我得到了以下错误:

java.lang.IllegalArgumentException:您无法为 破坏活动

因此,我使用
应用程序
上下文来解决我的错误

MyApplication.java是我的
Application

public class MyApplication extends Application {

    private static MyApplication mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
    }

    public static MyApplication getContext() {
        return mContext;
    }
}
不要忘记在
Manifests.xml
文件中声明应用程序:

<application
    android:name=".MyApplication"
    android:icon="@drawable/icon"
    android:label="@string/app_name" >
activity_main.xml是我的布局文件

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="alertImage"
            type="int" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp">

        <ImageView
            android:id="@+id/iv_image"
            android:layout_width="34dp"
            android:layout_height="34dp"
            imageDrawableRes="@{alertImage}"
            android:scaleType="fitXY"
            android:padding="4dp"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

您需要使用
@BindingAdapter({“bind:imageUrl”})
@BindingAdapter({“app:imageUrl”})
,这是一个小教程,请检查itI是否尝试了这两个选项。还是不行,奇怪的错误。我不明白为什么它不起作用。作为对@RaviRupareliya的响应,如果在BindingAdapter的参数中提供名称空间,则会收到额外的警告。它应该是@BindingAdapter(“imageUrl”)。@GeorgeMount知道了,但是如果我们有多个参数,是否需要它来提供名称空间?您可以为您的模型和视图模型添加代码吗?另外,查看一下图像视图的绑定可能会很有趣。它与您共享的代码不兼容。它工作的唯一方式是@BindingAdapter(“maleIcon”)和模型的属性具有相同的名称。
fun loadImage(layout: ConstraintLayout, url:String?)
public class MyApplication extends Application {

    private static MyApplication mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
    }

    public static MyApplication getContext() {
        return mContext;
    }
}
<application
    android:name=".MyApplication"
    android:icon="@drawable/icon"
    android:label="@string/app_name" >
@BindingAdapter(value = "imageDrawableRes")
public static void bindErrorImageView(ImageView imageView, int imageDrawableRes) {

    Glide.with(MyApplication.getContext())
        .load(imageDrawableRes)
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .centerCrop()
        .error(placeHolder)
        .placeholder(placeHolder)
        .into(imageView);

}
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="alertImage"
            type="int" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp">

        <ImageView
            android:id="@+id/iv_image"
            android:layout_width="34dp"
            android:layout_height="34dp"
            imageDrawableRes="@{alertImage}"
            android:scaleType="fitXY"
            android:padding="4dp"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
ActivityMainBinding binding = DataBindingUtil.inflate(inflater, R.layout.activity_main, null, false);
binding.setAlertImage(R.drawable.icn_info);