Android Databinding: “Method references using '.' is deprecated”

 ̄綄美尐妖づ 提交于 2020-01-01 08:02:52

问题


When using databinding in my app, I get the following warning when compiling:

Warning:Method references using '.' is deprecated. Instead of 'handler.onItemClick', use 'handler::onItemClick'

Please see my XML below.

<?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="handler" type="ClickHandler"/>
        <variable name="active" type="boolean"/>
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="@{!active ? handler.onItemClick : null}"
        android:background="@color/backgroundWhite"/>
    </RelativeLayout>
</layout>

Please note the : from the conditional statement

Pretty straightforward message, until I change the '.' to '::'.

android:onClick="@{!active ? handler::onItemClick : null}"

Since the onItemClick is inside a conditional statement, it seems to interpret the first of the two ::'s as the 'else' statement of the condition. On the second ':', I get the error:

<expr> expected, got ':'

EDIT: As @CommonsWare suggested in the comments, inverting the statement to "@{active ? null : handler::onItemClick}" doesn't help either, a similar error is shown (see comments)

EDIT2: Apparently, when stripping the conditional statement away, being left with "@{handler::onItemClick}", it still gives an error: '!=', '%', '*', '+', ',', '-', '.', '/', <, <<, <=, '==', '>', '>=', '>>', '>>>' or '[' expected, got ':' Using the dot-notation, still gives a warning when compiling

Is there any way to escape these ::'s, so it is interpreted correctly?


回答1:


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.




回答2:


The '::' error is currently an open bug for the Android Studio xml editor.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/37323648/android-databinding-method-references-using-is-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!