Kotlin – RecyclerView.ViewHolder子类 – 无法访问额外的属性

我已经实现了RecyclerView.ViewHolder子类如下:

class PersonViewHolder(itemView: View, binding: ViewDataBinding) : RecyclerView.ViewHolder(itemView) { } 

现在我试图在RecyclerView.Adapter的子类中像这样访问声明的绑定属性:

 override fun onBindViewHolder(holder: PersonViewHolder?, position: Int) { val person = persons[position] if (holder != null) { holder.binding.setVariable(BR.person, person) // line with error holder.binding.executePendingBindings() // line with error } } 

但编译器抱怨 – Unresolved reference: binding

这是完整的实现:

 class PersonsAdapter(private var persons: Array<Person>) : RecyclerView.Adapter<PersonsAdapter.PersonViewHolder>() { override fun onBindViewHolder(holder: PersonViewHolder?, position: Int) { val person = persons[position] if (holder != null) { holder.binding.setVariable(BR.person, person) holder.binding.executePendingBindings() } } override fun getItemCount(): Int { return persons.size } override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): PersonViewHolder { val itemView = LayoutInflater.from(parent!!.context).inflate(R.layout.list_item_person, parent, false) return PersonViewHolder(itemView, DataBindingUtil.bind(itemView)) } class PersonViewHolder(itemView: View, binding: ViewDataBinding) : RecyclerView.ViewHolder(itemView) { } } 

任何想法,如果我失去了什么在这里? 请建议。

Kotlin language will be the best programming language for Android.