问题
I am using following :
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);
}
}
here is my layout file
<?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 :
Error:(33, 25) Cannot find the setter for attribute 'app:onClick' with parameter type lambda on android.widget.Button.
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?
回答1:
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);
}
回答2:
Try to change your signature for your Binding method:
BindingAdapter({"eventName", "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);
}
}
来源:https://stackoverflow.com/questions/43806390/android-databinding-bindingadapter-error