Android 将参数传递给方法的数据绑定

Android Data Binding with parameter passing to method

First, this question is not a case of 'onClick' event parameter passing.

我有 DateUtil class,其中一种方法如下:

public static String formatDate(long date) {
        SimpleDateFormat dateFormat;
        dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        Calendar c = Calendar.getInstance();

        dateFormat.setTimeZone(TimeZone.getDefault());
        c.setTimeInMillis(date);
        return dateFormat.format(c.getTimeInMillis());
    }

我的模型 CommentEntity 具有以下属性:

 private int id;
 private int productId;
 private String text;
 private Date postedAt;

现在我在其中一种布局中显示评论。

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable name="comment"
                  type="com.example.entity.CommentEntity"/>
        <variable
        name="dateUtil"
        type="com.example.util.DateUtil"/>

    </data>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:layout_alignParentRight="true"
                android:layout_below="@id/item_comment_text"

                //This line gives error for data binding
                android:text="@{dateUtil.formatDate(comment.postedAt.time)}"/>
</layout>

我得到的错误是:

cannot find method formatDate(com.example.util.DateUtil) in class long

同样,如果我修改 formatDate() 方法,因为它默认使用当前时间,因此删除数据绑定中传递的参数,它将完美地工作。

所以我是做错了什么还是一个错误?

请提供在数据绑定中将参数传递给方法的问题的解决方案。

尝试以下方法:

  1. 不要直接从 xml[=43= 中的数据绑定获取 DateUtil class 对象]

在你的 CommentEntity 模型 class 中创建一个 BindingAdapter 方法,如下所示:

@BindingAdapter("android:text")
public static void setPaddingLeft(TextView view, long date) {
    view.setText(DateUtil.formatDate(long));
}

然后像下面那样使用 xml:

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:layout_alignParentRight="true"
            android:layout_below="@id/item_comment_text"
            android:text="@{comment.postedAt.time}"/>

解释:

当您想基于数据模型为您的视图应用一些自定义逻辑时,您需要使用 BindingAdapter 来完成这项工作。因此,您可以提供一些自定义标签或使用您需要设置逻辑的任何默认 android: 标签。

我拒绝您使用 DateUtil 绑定适配器,因为您可能也在其他地方使用它的方法。因此建议在您的模型中为该逻辑创建新方法,以便核心逻辑保持不变。 (尽管您可以将 DateUtils 用于此逻辑,您只需要将其设为 BindingAdapter)。

因为您想在 DateUtil 中使用静态方法,您应该导入它:

<data>
    <variable ... />
    <import type="foo.bar.DateUtil"/>
</data>

并在 TextView 中:

<TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:layout_alignParentRight="true"
                android:layout_below="@id/item_comment_text"
               //use DateUtil directly-
                android:text="@{DateUtil.formatDate(comment.postedAt.time)}"/>

你的错误是试图将它用作 variable - 这告诉数据绑定期望这个 class 实例绑定在你的 UI class 中的某个地方(Fargment/Activity)