按钮上的数据绑定 onclick 侦听器

Sus*_*ush 5 data-binding android

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

我需要的是我的自定义方法在单击按钮时调用“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/布局/activity_main.xml 位置:61:35 - 61:61 ****\ 数据绑定错误 ****

示例代码。

活动主文件

<?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>
Run Code Online (Sandbox Code Playgroud)

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;
}
}
Run Code Online (Sandbox Code Playgroud)

Khe*_*raj 1

用这个

 android:onClick="@{user}"
Run Code Online (Sandbox Code Playgroud)

因为你已经创造了BindingAdapter

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

    ...
}
Run Code Online (Sandbox Code Playgroud)

原因

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