首页 > 解决方案 > Android kotlin 两种方式数据绑定问题

问题描述

我正在尝试在回收站视图中使用数据绑定,但我遇到了问题。在数据变量上,我无法选择我的真实类型(相反,我可以选择类型作为默认包。然后我无法访问我的适配器类上的变量。我在线上的适配器出现错误:binding.curMovie = movie .我得到的错误是:无法访问类movieItem检查您的模块类路径是否缺少或冲突的依赖项绑定

电影项目.kt 文件

import com.google.gson.annotations.SerializedName

data class MovieItem(

    @SerializedName("popularity") val popularity: Double,
    @SerializedName("id") val id: Int,
    @SerializedName("video") val video: Boolean,
    @SerializedName("vote_count") val vote_count: Int,
    @SerializedName("vote_average") val vote_average: Double,
    @SerializedName("title") val title: String?,
    @SerializedName("release_date") val release_date: String?,
    @SerializedName("original_language") val original_language: String?,
    @SerializedName("original_title") val original_title: String?,
    @SerializedName("genre_ids") val genre_ids: List<Int>,
    @SerializedName("backdrop_path") val backdrop_path: String?,
    @SerializedName("adult") val adult: Boolean,
    @SerializedName("overview") val overview: String?,
    @SerializedName("poster_path") val poster_path: String?
)

list_item.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="curMovie"
            type="MovieItem" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <TextView
            android:id="@android:id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:minHeight="?android:attr/listPreferredItemHeightSmall"
            android:paddingStart="?android:attr/listPreferredItemPaddingStart"
            android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
            android:text="@{curMovie.title}"
            android:textAppearance="?android:attr/textAppearanceListItemSmall"
            app:layout_constraintBottom_toBottomOf="parent"
            tools:text="my movie" />


    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

电影适配器.kt 文件

class MovieAdapter() : RecyclerView.Adapter<MovieAdapter.MovieViewHolder>() {

    var moviesList: List<MovieItem> = ArrayList()



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieViewHolder {
        val inflater = LayoutInflater.from(parent.context)

        val binding = ListItemBinding.inflate(inflater)


        return MovieViewHolder(binding)

    }

    override fun getItemCount(): Int {
        return moviesList.size
    }

    override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
        val movie = moviesList.get(position)

        holder.setMovieData(movie)
    }

    class MovieViewHolder(val binding: ListItemBinding) : RecyclerView.ViewHolder(binding.root) {
        fun setMovieData(movie: MovieItem) {
            binding.curMovie = movie
        }
    }

    fun setMovieList(moviesList : List<MovieItem>){
        this.moviesList = moviesList
        notifyDataSetChanged()
    }

}

我的包文件

标签: androidkotlindata-binding

解决方案


最有可能的是,包名称以大写字母https://stackoverflow.com/a/54752522/11738109或在 list_item.xml 中使用完整的类路径而不是简短的 MovieItem


推荐阅读