When using databinding in my app, I get the following warning when compiling:
Warning:Method references using \'.\' is deprecated. Instead of \'handler.onItemClick
I didn't want to switch off Java 8 so I used the lambda expressions in databinding instead:
android:onClick="@{(v)->handler.onItemClick(v)}"
Here is an article by George Mount that gives lots of examples.
One thing to note is that the lambda expression is bound when the event occurs not at binding time.
You can still use JavaVersion.VERSION_1_8
.
Just use app:onClick
, and define a BindingAdapter like this:
@BindingAdapter("onClick")
public static void bindOnClick(View view, final Runnable runnable) {
view.setOnClickListener(v -> runnable.run());
}
Then you can use app:onClick="@{handler::onItemClick}"
without warnings or errors.
The '::' error is currently an open bug for the Android Studio xml editor.
My guess is that the deprecation warning is shown because Android Data Binding is currently not being fully compatible with Java 8.
Putting the following into your project's build.gradle
file should hide mentioned warning.
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
Unless you are using Java 8 features in your project.