使用数据绑定加载图像

时间:2016-11-07 12:35:17

标签: android android-databinding

我想使用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);
    }

我还应该提到" III","图像 - " + 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}"/>

并且maleIcon是一个url String

@SerializedName("male_icon")
@Expose
private String maleIcon;

4 个答案:

答案 0 :(得分:1)

Android DataBinding my_dict = { 'id': 10, 'city': 20, 'state': 30 } mymodel.objects.raw('''SELECT * from users where id = %(id)s and city = %(city)s and state = %(state)s ''', my_dict) 仅支持android命名空间或无。所以用

替换定义
@BindingAdapter

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

答案 1 :(得分:1)

以下是步骤:  1.在模型类中,在注解@BindAdapter(“ {bind:应该是此处获取图像网址的同一变量”})中声明此方法

  @BindingAdapter({"bind:imageUrl"})
public static void loadImage(ImageView view, String imageUrl) {
    Picasso.with(view.getContext())
            .load(imageUrl)
            .placeholder(R.drawable.placeholder)
            .into(view);}

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

3。)不要忘记添加,数据是对象的Bean类用户名`

    <variable
        name="user"
        type="com.brst.cocktailclub.beans.restaurantSearch.Datum"/>
</data>`

4。)还要在布局参数中添加xmlns:app =“ http://schemas.android.com/apk/res-auto”。

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

5。)记住您的Bean类应该扩展BaseObservable。

6。)最后在适配器中设置数据

 public void onBindViewHolder(@NonNull RestaurantAdapter.ViewHolder viewHolder, int i) {

    Datum datum=data.get(i);

    viewHolder.customRestaurantListBinding.setUser(datum);


}

答案 2 :(得分:0)

在此行:

fun loadImage(layout: ConstraintLayout, url:String)

?添加到网址类型。结果是:

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

在我的情况下,我将布局作为参数而不是图像视图。这应该起作用。

答案 3 :(得分:0)

我正在使用上述答案之一,但出现以下错误:

  

java.lang.IllegalArgumentException:您无法开始加载   破坏活动

所以我正在使用Application上下文来解决我的错误。

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

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

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

}

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>

MainActivity.class 是我的Activity类。

ActivityMainBinding binding = DataBindingUtil.inflate(inflater, R.layout.activity_main, null, false);
binding.setAlertImage(R.drawable.icn_info);