简体   繁体   English

Android - 绑定适配器不起作用

[英]Android - Binding adapter not working

I have create a binding adapter to display picture with picasso, but it doesn't work.我创建了一个绑定适配器来用毕加索显示图片,但它不起作用。 I have the following error :我有以下错误:

Found data binding errors.发现数据绑定错误。 ****/ data binding error ****msg:Cannot find the setter for attribute 'app:loadPicture' with parameter type java.lang.String on android.widget.ImageView. ****/ 数据绑定错误 ****msg:在 android.widget.ImageView 上找不到参数类型为 java.lang.String 的属性“app:loadPicture”的设置器。 file:/home/groupevsc.com/mathieu_labar/Documents/Projects/android-jetpack/app/src/main/res/layout/activity_detail_movie.xml loc:27:31 - 27:52 ****\\ data binding error ****文件:/home/groupevsc.com/mathieu_labar/Documents/Projects/android-jetpack/app/src/main/res/layout/activity_detail_movie.xml loc:27:31 - 27:52 ****\\数据绑定错误* ***

Here is my binding adapter :这是我的绑定适配器:

object CommonBindingUtil {

    @JvmStatic
    @BindingAdapter("loadPicture")
    fun loadPicture(view: ImageView, text: String) {
        Picasso.with(view.context)
                .load(text)
                .error(R.drawable.ic_movie_24)
                .fit()
                .placeholder(R.drawable.ic_movie_24)
                .into(view)
    }

}

And my XML has attribute "app:loadPicture" :我的 XML 具有属性 "app:loadPicture" :

<ImageView
    android:id="@+id/picture"
    android:layout_width="@dimen/material_image_simple_width"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:src="@drawable/ic_movie_24"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:loadPicture="@{viewModel.movie.Poster}"/>

Here is my GitHub repository: https://github.com/mlabar/android-jetpack/tree/tech_ajout_databinding这是我的 GitHub 存储库: https : //github.com/mlabar/android-jetpack/tree/tech_ajout_databinding

Would anyone have an idea to solve my problem?有人会想办法解决我的问题吗?

Thank you!谢谢!

谢谢@Blackbelt 解决了我的问题,我在所有build.gradle模块中添加了“kotlin-kapt”:

apply plugin: 'kotlin-kapt'

Here is my code这是我的代码

@JvmStatic
    @BindingAdapter({"bind:loadPicture"})
    fun loadPicture(view: ImageView, loadPicture: String) {
        Picasso.with(view.context)
                .load(loadPicture)
                .error(R.drawable.ic_movie_24)
                .fit()
                .placeholder(R.drawable.ic_movie_24)
                .into(view)
    }

for more details see my project on GitHub有关更多详细信息,请参阅我在GitHub 上的项目

Change your MovieViewModel class like this:像这样改变你的MovieViewModel类:

class MovieViewModel(private val movie: Movie) : Observer, BaseObservable() {

    init {
       Movie.addObserver(this)
    }
    val Poster: String
        @Bindable get() {
            return Poster.name
        }
}

And Movie class like this:像这样的电影类:

class Movie: Observable() {
    var Title: String = ""
        set(value) {
            field = value
            setChangedAndNotify("Title")
        }
    var Year: String = ""
        set(value) {
            field = value
            setChangedAndNotify("Year")
        }
    var imdbID: String = ""
        set(value) {
            field = value
            setChangedAndNotify("imdbID")
        }
    var Type: String = ""
        set(value) {
            field = value
            setChangedAndNotify("Type")
        }
    var Poster: String = ""
        set(value) {
            field = value
            setChangedAndNotify("Poster")
        }
    var Plot: String = ""
        set(value) {
            field = value
            setChangedAndNotify("Plot")
        }

    private fun setChangedAndNotify(field: Any) {
        setChanged()
        notifyObservers(field)
    }
}

Make your class context according to your need, run it hope it'll solve your problem.根据您的需要创建您的类上下文,运行它希望它会解决您的问题。

You are not passing the right parameters for binding adapter function.您没有为绑定适配器函数传递正确的参数。

object CommonBindingUtil {对象 CommonBindingUtil {

@JvmStatic
@BindingAdapter("loadPicture")
fun loadPicture(view: ImageView, text: String) {
    Picasso.with(view.context)
            .load(text)
            .error(R.drawable.ic_movie_24)
            .fit()
            .placeholder(R.drawable.ic_movie_24)
            .into(view)
}

} }

Here loadPicture function expects parameters imageView and a String, but here, in your xml your are passing the Object of photo这里loadPicture函数预期参数imageView和一个字符串,但在这里,在你的XML,您将可以通过照片的对象

app:loadPicture="@{viewModel.movie.Poster}

You should instead write something like this app:loadPicture="@{viewModel.movie.Poster.url}你应该写这样的东西app:loadPicture="@{viewModel.movie.Poster.url}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM