相关疑难解决方法(0)

使用Android中的数据绑定在ImageView的android:src中设置可绘制资源ID

我正在尝试使用数据绑定将drawable资源ID设置为ImageView的android:src

这是我的对象:

public class Recipe implements Parcelable {
    public final int imageResource; // resource ID (e.g. R.drawable.some_image)
    public final String title;
    // ...

    public Recipe(int imageResource, String title /* ... */) {
        this.imageResource = imageResource;
        this.title = title;
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="recipe"
            type="com.example.android.fivewaystocookeggs.Recipe" />
    </data>

    <!-- ... -->

    <ImageView
        android:id="@+id/recipe_image_view"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@{recipe.imageResource}" />

    <!-- ... -->

</layout>
Run Code Online (Sandbox Code Playgroud)

最后,活动类:

// ...

public class RecipeActivity extends AppCompatActivity {

    public static …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-databinding

72
推荐指数
8
解决办法
5万
查看次数

数据绑定android更新imageview

嗨,我尝试用数据绑定更新imageview,但我想知道如何正确地做到这一点.

我的变种

@BindingAdapter({"bind:someImage"})
        public  void loadIt(ImageView view, String imageUrl) {
            Picasso.with(view.getContext())
                    .load(getSomeImage())
                    .placeholder(android.R.drawable.alert_dark_frame)
                    .into(view);
}

 public void setSomeImage(){
        notifyPropertyChanged(BR.someImage);
    }
Run Code Online (Sandbox Code Playgroud)

我的布局

<ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imagePath"
      app:imageUrl="@{var.someImage}"/>
Run Code Online (Sandbox Code Playgroud)

我有搜索但找不到解决方案.... links = link1,Link2,Link 3

data-binding android android-databinding

0
推荐指数
1
解决办法
947
查看次数