简体   繁体   中英

Android Data Binding Library lambda for custom Listener

This code works just 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 custom listener like this:

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 big suprise :( ), is it even possible to use lambda with custom listener?

It is OK with gradle 2.2.0 version:

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>

Maybe that was an older version of data-binding library, which cause your error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM