本文介绍了Android数据绑定BindingAdapter错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用以下内容:

公共类BindingAdapter {

public class BindingAdapter {

@android.databinding.BindingAdapter({"app:eventName", "app:onClick"})
public static void setColorChangeListener(final View view,
                                          final String eventName,
                                          View.OnClickListener newListener) {
    CompositeClickListener c = new CompositeClickListener();
    c.addOnClickListener(newListener);
    View.OnClickListener a = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Toast.makeText(view.getContext(), eventName, Toast.LENGTH_SHORT).show();
        }
    };
    c.addOnClickListener(a);
    view.setOnClickListener(c);
}
}

这是我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
    <import type="android.widget.Toast" />

     <variable
         name="namewa"
         type="String" />

        <variable
            name="namewa2"
            type="String" />
        <!--<variable
            name="banner"
            type="games.kapow.android.ui.view_models.BannerViewModel" />-->
    </data>
<android.support.constraint.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.arjitagarwal.helloworld.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:eventName="@{namewa}"
        app:onClick= "@{(v) -> Toast.makeText(v.getContext(), namewa, Toast.LENGTH_LONG).show()}"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

</layout>

构建项目时出现此错误:

I am getting this error while building the project :

但是,如果我同时从绑定适配器和布局中删除eventName东西,它就可以正常工作.

However, if I remove eventName thing from both binding adapter and layout it just works fine.

https://developer.android.com/reference/android/databinding/BindingAdapter.html

上面的链接以相同的方式执行此操作.我在做什么错了?

The above link does this in same manner. What I am doing wrong?

推荐答案

注释中的细微变化对我有用.默认情况下requireAll为true.

A slight change in the annotation is working for me. By Default requireAll is true.

@BindingAdapter(value = {"eventName", "onClick"}, requireAll = false)
public static void setColorChangeListener(final View view,
                                          final String eventName,
                                          View.OnClickListener newListener) {
    CompositeClickListener c = new CompositeClickListener();
    c.addOnClickListener(newListener);
    View.OnClickListener a = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Toast.makeText(view.getContext(), eventName, Toast.LENGTH_SHORT).show();
        }
    };
    c.addOnClickListener(a);
    view.setOnClickListener(c);
}

这篇关于Android数据绑定BindingAdapter错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 14:44