简体   繁体   中英

@BindingAdapter not working with Android Kotlin library

I've this BindingAdapter to load image using Glide in my library module

import android.widget.ImageView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide

@BindingAdapter("imageUrl")
fun loadImage(view: ImageView, imageUrl: String) {
    Glide.with(view)
        .load(imageUrl)
        .into(view)
}

and I tried to use the adapter like this

   <ImageView
            ...
            app:imageUrl="@{`http://pngimg.com/uploads/alfa_romeo/alfa_romeo_PNG75.png`}"
            ... />

but am getting

****/ data binding error ****msg:Cannot find the setter for attribute 'app:imageUrl' with parameter type java.lang.String on android.widget.ImageView.

The weird thing is, when I convert the BindingAdapter to Java from Kotlin , it works.

public class ImageViewBindingAdapter {

    @BindingAdapter("imageUrl")
    public static void setImageUrl(ImageView view, String url) {
        Glide.with(view)
                .load(url)
                .into(view);
    }
}

NOTE: This issue only exist with the library module. App module works perfectly fine with the Kotlin file.

What am I doing wrong ?

Duplicated: https://stackoverflow.com/a/52668004/1607169

TL;DR:

apply plugin: 'kotlin-kapt'
@BindingAdapter("imageUrl") 

代替

@BindingAdapter("app:imageUrl")

your code should be like this

object BindUtil {
  @JvmStatic
  @BindingAdapter("app:imageUrl")
  fun imageUrl(view: ImageView, imageUrl: String?) {
      Glide.with(view)
       .load(imageUrl)
       .into(view)
  }
}

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