Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda