Android BindingConversion 多个参数

Android BindingConversion multiple parameters

我写了这个 BindingConversion:

@BindingConversion
public static String convertDateToFormat(Date date, String format) {
    return new SimpleDateFormat(format).format(date);
}

在我的布局文件中:

<TextView
 android:text="@{BindingUtils.convertDateToFormat(schedule.dateStart, `EEEE`)}"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/textView2"/>

但是构建失败。

谁能解释一下如何将多个参数传递给和绑定转换?

来自Androiddocumation

The converter should take one parameter, the expression type, and the return value should be the target value type used in the setter

目前不支持有参数。

您可以改为使用 BindingAdapter:

@BindingAdapter({ "date", "format" })
public static void bindDate(final TextView view, final ZonedDateTime date, final String format) {
    if (date != null) {
        view.setText(DateTimeFormatter.ofPattern(format).format(date));
    }
}