具有数据绑定的约束布局

时间:2018-06-13 07:15:09

标签: android

当使用简单的约束布局时,它工作正常,但是当使用数据绑定时它没有运行,并且预览是错误的,并且绑定方法也没有找到显示错误 **发现数据绑定错误。 **** /数据绑定错误****消息:侦听器类android.view.View.OnClickListener与方法onClick不匹配任何方法项的签名:: onCardClick / app / src / main / res / layout / activity_main。 xml loc:16:27 - 16:43 **** \ data binding error ******

没有数据绑定

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    android:id="@+id/linearLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="24dp"
    android:paddingRight="24dp"
    android:paddingTop="14dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:text="Hello First"
        app:layout_constraintBottom_toTopOf="@id/button"
        app:layout_constraintEnd_toStartOf="@id/imageView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/imageView"/>

    <TextView
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:text="Hello Second"
        app:layout_constraintBottom_toBottomOf="@id/imageView"
        app:layout_constraintEnd_toStartOf="@id/imageView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/line"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1"
        android:adjustViewBounds="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher_round"/>

    <TextView
        android:id="@+id/line"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="14dp"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageView"/>
</android.support.constraint.ConstraintLayout>

enter image description here 使用数据绑定

<?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">

    <data>

        <variable
            name="item"
            type="com.rv.cnstnt_example.MyPojoClass"/>
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="24dp"
        android:paddingRight="24dp"
        android:onClick="@{item::onCardClick}"
        android:paddingTop="14dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp"
            android:text="@{item.name}"
            app:layout_constraintBottom_toTopOf="@id/button"
            app:layout_constraintEnd_toStartOf="@id/imageView"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@id/imageView"/>

        <TextView
            android:id="@+id/button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp"
            android:text="@{item.dob}"
            app:layout_constraintBottom_toBottomOf="@id/imageView"
            app:layout_constraintEnd_toStartOf="@id/imageView"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/textView"/>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:layout_constraintBottom_toTopOf="@id/line"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@mipmap/ic_launcher_round"/>

        <TextView
            android:id="@+id/line"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="14dp"
            android:background="@color/colorPrimaryDark"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/imageView"/>
    </android.support.constraint.ConstraintLayout>
</layout>

enter image description here MyPojoClass.java

   public class MyPojoClass
{
    private String name = "Vineet";
    private String dob = "22 jun 2018";

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getDob()
    {
        return dob;
    }

    public void setDob(String dob)
    {
        this.dob = dob;
    }

    public void onCardClick(){
        Log.e("DATA","CLICK");
    }
}

1 个答案:

答案 0 :(得分:0)

MyPojoClass类中的

将方法onCardClick更改为接受View作为参数。它应该是

public void onCardClick(View view) { 
     Log.e("DATA","CLICK"); 
}