【问题标题】:Databinding onclick listener on button按钮上的数据绑定 onclick 侦听器
【发布时间】:2017-11-18 08:46:25
【问题描述】:

试图理解数据绑定,但没有得到它。任何帮助表示赞赏。

我需要的是在单击按钮时我的自定义方法调用“setOnClick(User user)”。我只想了解绑定适配器的概念。

任务“:databinding:compileDebugJavaWithJavac”执行失败。 android.databinding.tool.util.LoggedErrorException:发现数据绑定错误。 ****/ 数据绑定错误 ****msg:在类 com.locale.databinding.MyAdapter 文件中找不到方法 setOnClick():/Users/svernekar003/Documents/GitHub/RxDagger2Demo/databinding/src/main/res/ layout/activity_main.xml loc:61:35 - 61:61 ****\数据绑定错误****

示例代码。

activity_main.xml

<?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"
xmlns:tools="http://schemas.android.com/tools">
<data >

    <variable
        name="user"
        type="com.locale.databinding.User"></variable>

    <variable
        name="myClickHandler"
        type="com.locale.databinding.MyAdapter"></variable>
</data>

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.locale.svernekar.databinding.MainActivity">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:text="@{user.name}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/last_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="@{user.lastName}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="@+id/name"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.502" />

    <ViewStub
        android:id="@+id/view_stub"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:inflatedId="@+id/inflate_id"
        android:layout="@layout/view_stub_sample" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:onClick="@{()->myClickHandler.setOnClick(user)}"
        android:text="Change Data"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/last_name" />

</android.support.constraint.ConstraintLayout>

MyAdapter.java

public class MyAdapter {

private User user;

@BindingAdapter("android:onClick")
public static void setOnClick(User user) {

    Log.d("Onclick", "after do long time");
}


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

public int getId() {
    return android.R.drawable.ic_dialog_dialer;
}
}

【问题讨论】:

标签: android data-binding


【解决方案1】:

您在这里缺少对回调的理解。我建议在这里做谷歌的数据绑定代码实验室:

https://codelabs.developers.google.com/codelabs/android-databinding/index.html

但是,我们可以一起完成解决方案。

首先,要创建一个BindingAdapter,你需要在方法中指定一个视图类型。所以,它应该看起来像这样:

@BindingAdapter("android:onClick")
fun setOnClick(view: View, user: User) {
    Log.d("Onclick", "after do long time")
}

现在,关于 BindingAdapters 的事情是,它们要么在页面加载时设置,要么在其绑定值更改时设置。因此,如果 User 是 LiveData,这将起作用,但是当用户单击时它不会调用 - 它会在用户值更新时调用。 BindingAdapter 在绑定值更新时调用,无论名称是什么。所以,上面的代码和这个块是一样的:

@BindingAdapter("app:userUpdated")
fun setUserUpdated(view: View, user: User) {
    Log.d("UserUpdated", "after user value is updated")
}

在这种情况下,您的 onClick 绑定没有设置点击侦听器 - 它正在设置与“用户”字段的绑定。因此,onClick 将有两个 bindingadapter——一个如果你绑定一个 User,一个(系统默认)如果你绑定一个 ClickListener。您绑定到 clicklistener 的 XML,而不是用户,因此您的适配器将永远不会被执行。但即使它被正确绑定,它也不会在点击时绑定,因为它不是那样绑定的。要让它在点击时为用户绑定,您需要执行以下操作:

@BindingAdapter("android:onClick")
fun setOnClick(view: View, user: User) {
    view.setOnClickListener(View.OnClickListener { Log.d("Onclick", "after do long time") })
}

【讨论】:

  • 应该选择答案。
  • 这样做有什么注意事项吗?
【解决方案2】:

使用这个

 android:onClick="@{user}"

因为你创建了BindingAdapter

@BindingAdapter("android:onClick")
public static void setOnClick(User user) {

    ...
}

原因

您已经创建了属性android:onClick@BindingAdapter,因此它将像上面一样工作。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-20
  • 2011-07-20
  • 1970-01-01
  • 2014-02-26
  • 2011-11-27
  • 2017-08-27
  • 1970-01-01
相关资源
最近更新 更多