内部 class ViewHolder 的构造函数只能与包含 class 的接收者一起调用
Constructor of inner class ViewHolder can be called only with receiver of containing class
我想使用 RxJava2 在 RecyclerView 项目上设置一个侦听器。这些项目是复选框。我想分别听每个项目。
所以我收到一个错误 Constructor of inner class ViewHolder can be called only with receiver of containing class in the
return TraceAdapter.ViewHolder(view)
class TraceAdapter(private var checkList: List<TraceViewModelRow> = listOf()) :
RecyclerView.Adapter<TraceAdapter.ViewHolder>() {
private val publishSubject = PublishSubject.create<Event>()
val events: Observable<Event> = publishSubject
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TraceAdapter.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_trace_task, parent, false)
return TraceAdapter.ViewHolder(view)
}
override fun getItemCount(): Int = checkList.size
override fun onBindViewHolder(holder: TraceAdapter.ViewHolder, position: Int) {
holder.bindTraceList(checkList[position])
}
override fun onViewRecycled(holder: ViewHolder) {
super.onViewRecycled(holder)
}
inner class ViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView), LayoutContainer {
fun bindTraceList(trace: TraceViewModelRow) {
with(trace) {
checkbox_itemText.clicks()
.map { checkList[layoutPosition] }
.subscribe { publishSubject }
checkbox_itemText.text = description
checkbox_itemText.isChecked = isChecked
}
}
}
}
我认为你只需要更改这一行:
return TraceAdapter.ViewHolder(view)
对此:
return ViewHolder(view)
由于您已明确将其标记为内部 class,因此它需要外部 class 的一个实例,默认情况下您会获得该实例,因为您是从外部 class 中构造它的=28=]。但是,前提是您不使用外部 class 类型作为前缀,如上所示。
内部和嵌套 classes 之间的区别在 Kotlin 和 Java 中有点不同(Java 使用术语 static
表示嵌套 class没有外部实例的 es class)。有更详细的解释 here.
我想使用 RxJava2 在 RecyclerView 项目上设置一个侦听器。这些项目是复选框。我想分别听每个项目。 所以我收到一个错误 Constructor of inner class ViewHolder can be called only with receiver of containing class in the
return TraceAdapter.ViewHolder(view)
class TraceAdapter(private var checkList: List<TraceViewModelRow> = listOf()) :
RecyclerView.Adapter<TraceAdapter.ViewHolder>() {
private val publishSubject = PublishSubject.create<Event>()
val events: Observable<Event> = publishSubject
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TraceAdapter.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_trace_task, parent, false)
return TraceAdapter.ViewHolder(view)
}
override fun getItemCount(): Int = checkList.size
override fun onBindViewHolder(holder: TraceAdapter.ViewHolder, position: Int) {
holder.bindTraceList(checkList[position])
}
override fun onViewRecycled(holder: ViewHolder) {
super.onViewRecycled(holder)
}
inner class ViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView), LayoutContainer {
fun bindTraceList(trace: TraceViewModelRow) {
with(trace) {
checkbox_itemText.clicks()
.map { checkList[layoutPosition] }
.subscribe { publishSubject }
checkbox_itemText.text = description
checkbox_itemText.isChecked = isChecked
}
}
}
}
我认为你只需要更改这一行:
return TraceAdapter.ViewHolder(view)
对此:
return ViewHolder(view)
由于您已明确将其标记为内部 class,因此它需要外部 class 的一个实例,默认情况下您会获得该实例,因为您是从外部 class 中构造它的=28=]。但是,前提是您不使用外部 class 类型作为前缀,如上所示。
内部和嵌套 classes 之间的区别在 Kotlin 和 Java 中有点不同(Java 使用术语 static
表示嵌套 class没有外部实例的 es class)。有更详细的解释 here.