无法解析 RecyclerView 中的 Viewholder

Cant resolve View Holder in RecyclerView

我是 RecyclerView 的新手,我想实现它,但问题是我无法在以下代码中解析 viewholder 引用,请帮助我

class ViewAdapter : RecyclerView.Adapter<ViewAdapter.ViewHolder>(){
     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewAdapter.ViewHolder {
        // return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.slider, parent, false))
    // }
     }

     override fun getItemCount(): Int {
         TODO("not implemented") 
     }

     override fun onBindViewHolder(holder: ViewAdapter.ViewHolder, position: Int) {
         TODO("not implemented") 
     }


 }

您需要在 ViewAdapter class

中创建一个 class ViewHolder

示例代码

class ViewAdapter : RecyclerView.Adapter<ViewAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewAdapter.ViewHolder {
        // return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.slider, parent, false))
        // }
    }

    override fun getItemCount(): Int {
        TODO("not implemented")
    }

    override fun onBindViewHolder(holder: ViewAdapter.ViewHolder, position: Int) {
        TODO("not implemented")
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    }
}

您还没有在 ViewAdapter

中定义 ViewHolder class

只需在 ViewAdapter

中定义 ViewHolder class
class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {

}

ViewAdapter 是您正在创建的 class,因此 ViewAdapter.ViewHolder 是它的内部 class。

您需要创建这个内部 class,例如:

class ViewAdapter : RecyclerView.Adapter<ViewAdapter.ViewHolder>(){

   ...

   class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        ...
   }
}