【问题标题】:Could I have two parameters in one attribute for android data binding?我可以在一个属性中有两个参数用于 android 数据绑定吗?
【发布时间】:2016-09-01 04:15:45
【问题描述】:

我已阅读此博客中的自定义设置器。 https://medium.com/google-developers/android-data-binding-custom-setters-55a25a7aea47#.7ylt58pzz

我想知道我是否可以使用像下面这样的东西

在xml中

android:imageUrl="@{param1,param2}"

在java类中

@BindingAdapter(value={"imageUrl"}, requireAll=false)
public static void setImageUrl(ImageView imageView, String url,
        Drawable placeHolder) {
    if (url == null) {
        imageView.setImageDrawable(placeholder);
    } else {
        MyImageLoader.loadInto(imageView, url, placeholder);
    }
}

一个属性在视图内有两个参数。由于我在 android 数据绑定中看到了 lambdas 表达式(实际上它并没有真正使用 JDK 8 编译)https://developer.android.com/topic/libraries/data-binding/index.html#listener_binding,因此可以在处理程序事件中添加任务变量。

android:onClick="@{(theView) -> presenter.onSaveClick(theView, task)}"

自定义绑定方法中是否可以有两个(或更多)参数?

【问题讨论】:

标签: android android-databinding


【解决方案1】:

它也有效。

@BindingAdapter({"imageUrl", "error"})
public static void imageLoader(ImageView imageView, String url, Drawable error) {
    Picasso.with(imageView.getContext()).load(url).into(imageView);
}

<ImageView
    android:layout_margin="@dimen/activity_horizontal_margin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:scaleType="centerInside"
    android:clickable="false"
    app:imageUrl='@{imgUrl ?? "http://ww1.sinaimg.cn/large/7a8aed7bjw1f2zwrqkmwoj20f00lg0v7.jpg"}'
    app:error="@{@drawable/ic_image_load_error}"
    />

【讨论】:

    猜你喜欢
    • 2011-05-18
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 2016-06-30
    相关资源
    最近更新 更多