我尝试从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
}
}````
2条答案
按热度按时间0dxa2lsx1#
在你的
onBindViewHolder
中,你应该使用你的特定ViewHolder
,即LaunchesViewHolder
而不是RecyclerView.ViewHolder
。请看下面的代码。已编辑:还需要指定要覆盖的类
ki1q1bka2#
它使用(保持器作为LaunchesViewHolder).bind(数据集[位置])而不是holder.bind(数据集[位置])
查看更多详细信息https://www.section.io/engineering-education/implementing-multiple-viewholders-in-android-using-kotlin/