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

前端 未结 4 2212
悲哀的现实
悲哀的现实 2021-02-19 07:34

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

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

相关标签:
4条回答
  • 2021-02-19 08:00

    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.

    0 讨论(0)
  • 2021-02-19 08:00

    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.

    0 讨论(0)
  • 2021-02-19 08:06

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

    0 讨论(0)
  • 2021-02-19 08:10

    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.

    0 讨论(0)
提交回复
热议问题