Android lambda data binding library for custom listener

This code works fine:

Adapter Code:

@BindingAdapter({"app:onClick"})
public static void setOnClick(My view, View.OnClickListener onClickListener)
{
   view.addOnClickListener(onClickListener);
}

Xml code:

app:onClick="@{ (v) -> view.onClick(v) }"

But I need to make a custom listener as follows:

Adapter Code:

@BindingAdapter({"app:onClose"})
public static void setOnClose(My view, My.OnCloseListener onCloseListener)
{
   view.addOnCloseListener(onCloseListener);
}

Xml code:

app:onClose="@{ (x, y) -> view.onClose(x, y) }"

Listener Code:

public interface OnCloseListener
{
    void onClose(My x, int y);
}

In this case, the application does not compile (not a big surprise :(), is it possible to use lambda with a custom listener?

+4
source share
1 answer

OK with gradle version 2.2.0:

public interface ITest {
    void apply(String apply);
}

@BindingAdapter({"test"})
public static void testAdapter(View view, ITest test){
}

public interface IViewModel extends Observable {
    void accept(String string);
}
 
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="vm"
            type="com.example.mvvm.IViewModel"
            />
    </data>

    <View
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:test="@{vm::accept}"
        />

</layout>

It may have been an older version of the data binding library that is causing your error.

+1
source

Source: https://habr.com/ru/post/1649340/


All Articles