android - 在android :src for ImageView using data binding in Android中设置drawable资源ID

标签 android android-layout android-databinding

我正在尝试使用数据绑定(bind)将可绘制资源 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;
    }

    // ...
}

这是我的布局:

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

最后, Activity 类:

// ...

public class RecipeActivity extends AppCompatActivity {

    public static final String RECIPE_PARCELABLE = "recipe_parcelable";
    private Recipe mRecipe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mRecipe = getIntent().getParcelableExtra(RECIPE_PARCELABLE);
        ActivityRecipeBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_recipe);
        binding.setRecipe(mRecipe);
    }

    // ...

}

它根本不显示图像。我做错了什么?

顺便说一句,它与标准方式完美配合:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipe);

    final ImageView recipeImageView = (ImageView) findViewById(R.id.recipe_image_view);
    recipeImageView.setImageResource(mRecipe.imageResource);

}

最佳答案

答案截至 2016 年 11 月 10 日

Splash 下面的评论强调了没有必要使用自定义属性类型(如 imageResource),我们可以像这样为 android:src 创建多个方法:

public class DataBindingAdapters {

    @BindingAdapter("android:src")
    public static void setImageUri(ImageView view, String imageUri) {
        if (imageUri == null) {
            view.setImageURI(null);
        } else {
            view.setImageURI(Uri.parse(imageUri));
        }
    }

    @BindingAdapter("android:src")
    public static void setImageUri(ImageView view, Uri imageUri) {
        view.setImageURI(imageUri);
    }

    @BindingAdapter("android:src")
    public static void setImageDrawable(ImageView view, Drawable drawable) {
        view.setImageDrawable(drawable);
    }

    @BindingAdapter("android:src")
    public static void setImageResource(ImageView imageView, int resource){
        imageView.setImageResource(resource);
    }
}

旧答案

您总是可以尝试使用适配器:

public class DataBindingAdapters {

    @BindingAdapter("imageResource")
    public static void setImageResource(ImageView imageView, int resource){
        imageView.setImageResource(resource);
    }
}

然后您可以像这样在您的 xml 中使用适配器

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

请务必注意 xml 中的名称与 BindingAdapter 注解 (imageResource) 匹配

DataBindingAdapters 类不需要在任何地方特别声明,DataBinding 机制无论如何都会找到它(我相信)

关于android - 在android :src for ImageView using data binding in Android中设置drawable资源ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35809290/

相关文章:

android - 当 numColumns 更改时,如何在 gridview 上保留所选项目突出显示?

android - 如何在约束布局中创建更小的按钮

java - 如何将图像从 Java 应用程序发送到 Android 应用程序?

android - ExceptionInInitializerError 异常

java - 安卓代码: clarify button clicking facebook url must display

java - 未知的数据绑定(bind)类

android - 如何将数据绑定(bind)与 ProgressBar 结合使用?

android - 使用数据绑定(bind)库更新 UI

java - 如何在我的 Android 应用程序中更改(线程的)堆栈大小?

java -/storage/emulated/0/Notes/(没有这样的文件或目录)?