Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
Higher order function in BindingAdapter

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?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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)
    }
}
over 4 years ago · Santiago Trujillo Report

0

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)
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!