3

I am using DataBinding in android and I have a custom view: CarouselView

I wrote a binding adapter for that:

@BindingAdapter("onClick")
fun setOnClick(carouselView: CarouselView, onClick: (position: Int) -> Unit) {
    carouselView.setImageClickListener { position ->
        onClick.run(position)
    }
}

And in the xml:

<com.synnapps.carouselview.CarouselView
            android:id="@+id/carouselView"
            ...
            app:onClick="@{(p) -> vm.onAdsClicked(p)}"/>

But it does not compile. So I saw this answer in the Stackoverflow. But my problem is that I cannot use Runnable instead of kotlin hoc function because I need to pas a parameter to function.

How can I solve that?

Saman Sattari
  • 3,322
  • 6
  • 30
  • 46

2 Answers2

11

If I may, I had the same issue (passing a param inside a HOF in a BindingAdapter) and came up with a more elegant solution. You can indeed make this work, you just need define the HOF as a variable first.

ViewModel:

val onAdsClicked = fun(position: Int) {
// Do stuff
}

XML:

<com.synnapps.carouselview.CarouselView
            android:id="@+id/carouselView"
            ...
            app:onClick="@{vm.onAdsClicked}"/>

BindingAdapter:

@BindingAdapter("onClick")
fun setOnClick(carouselView: CarouselView, onClick: (position: Int) -> Unit) {
    carouselView.setImageClickListener { position ->
        onClick(position)
    }
}
Bogdan Zurac
  • 6,348
  • 11
  • 48
  • 96
  • but i don't see how to pass this parameter in xml? – Saman Sattari Mar 05 '19 at 14:30
  • I'm not completely sure I understand your question. If you mean passing the `position` parameter inside XML, you don't need to. It is automatically passed by the `BindingAdapter`. LE: Oh, my bad, I forgot to erase the `run()` call on the lambda. Try now. – Bogdan Zurac Mar 06 '19 at 06:40
  • 1
    FYI, added an issue to track this in the DataBinding issue tracker over at https://issuetracker.google.com/issues/131663967 – Bogdan Zurac Apr 30 '19 at 14:08
  • I have a problem here, when I use this approach, my livedata values gets erased.. – Andrew Sep 14 '20 at 19:29
3

Switching from hoc func to the the listener of this custom view worked for me:

import com.synnapps.carouselview.ImageClickListener

@BindingAdapter("onClick")
fun setOnClick(carouselView: CarouselView, imageClickListener: ImageClickListener) {
    carouselView.setImageClickListener(imageClickListener)
}
Saman Sattari
  • 3,322
  • 6
  • 30
  • 46
  • @Andrew `app:onClick="@{(p) -> vm.onAdsClicked(p)}"` – Saman Sattari Sep 14 '20 at 20:15
  • And how exactly do you define the listener in viewmodel? Wouldn't you get a reference to the view and therefore break the whole intention of viewmodel? And what is "p"? – Andrew Sep 14 '20 at 20:19
  • @Andrew, no. if you see I'm just passing position. not view. so the code in VM is just a function named `onAdsClicked` with one parameter: `position: Int` – Saman Sattari Sep 16 '20 at 07:06