kotlin 我可以通过适配器访问ViewHolder中的乐趣

igetnqfo  于 2022-11-16  发布在  Kotlin
关注(0)|答案(2)|浏览(148)

我尝试从onBindViewHolder()调用内部类LaunchesViewHolder中声明的fun bind,但收到错误“Unresolved resource bind”。我尝试使用其他变量x,只是为了查看,同样的问题

class LaunchesAdapter(private val dataSet: List<LaunchItem>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

   inner class LaunchesViewHolder( val binding: LaunchesItemLayoutBinding) :
        RecyclerView.ViewHolder(binding.root) {
        val x = 0
        public fun bind(currentLaunch: LaunchItem) {
           //do something
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

        return LaunchesViewHolder(
            LaunchesItemLayoutBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
     holder.bind(dataSet[position]) => error unresolved resource bind 
     holder.x =1 => error unresolved resource x
}

    override fun getItemCount(): Int {
        return dataSet.size
    }
}````
0dxa2lsx

0dxa2lsx1#

在你的onBindViewHolder中,你应该使用你的特定ViewHolder,即LaunchesViewHolder而不是RecyclerView.ViewHolder。请看下面的代码。

override fun onBindViewHolder(holder: LaunchesViewHolder, position: Int) {
    holder.bind(dataSet[position])
}

已编辑:还需要指定要覆盖的类

class LaunchesAdapter(private val dataSet: List<LaunchItem>) :
    RecyclerView.Adapter<LaunchesAdapter.LaunchesViewHolder>() {
}
ki1q1bka

ki1q1bka2#

它使用(保持器作为LaunchesViewHolder).bind(数据集[位置])而不是holder.bind(数据集[位置])
查看更多详细信息https://www.section.io/engineering-education/implementing-multiple-viewholders-in-android-using-kotlin/

相关问题