With Android databinding, I can do the following:
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{() -> viewModel.onClick()}" />
My ViewModel
does not have to implement OnClickListener
, but just have a method:
public void onClick() {
}
What it passes to the onClick
attribute in the Xml looks like a lambda to me.
How can I do this with my own BindingAdapters
?
What I want:
Let's assume I want to bind touch events and I want to pass the MotionEvent
, I would imagine this to look in the Xml like this:
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:onTouch="(view, event) -> viewModel.onTouch(event)" />
and the BindingAdapter
something like:
@BindingAdapter("onTouch")
public static void onTouch(View view, ??? lambda) {
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(final View v, final MotionEvent event) {
return lambda(event);
}
});
}
I do not want my ViewModel to implement OnTouchListener
and bind it like:
@BindingAdapter()
public static void onTouch(View view, View.OnTouchListener onTouchListener) {
view.setOnTouchListener(onTouchListener);
}
and I do not want bind the touch event directly to my ViewModel
like:
@BindingAdapter()
public static void onTouch(final View view, final MyViewModel myViewModel) {
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(final View v, final MotionEvent event) {
return myViewModel.onTouch(view, myViewModel);
}
});
}
Is this possible with Databinding?