我已经定义了这样的类(class)
abstract class MvpViewHolder<P>(itemView: View) : RecyclerView.ViewHolder(itemView) where P : BasePresenter<out Any?, out Any?> {
protected var presenter: P? = null
fun bindPresenter(presenter: P): Unit {
this.presenter = presenter
presenter.bindView(itemView)
}
}
其中
presenter.bindView(itemView)
给我一个错误,指出Type mismatch, required: Nothing, found: View!
。我已经在bindView
类中定义了presenter
,如下所示abstract class BasePresenter<M, V> {
var view: WeakReference<V>? = null
var model: M? = null
fun bindView(view: V) {
this.view = WeakReference(view)
}
}
它采用
view: V
的值。我尝试使用星型语法
BasePresenter<out Any?, out Any?>
定义BasePresenter<*,*>
的扩展名,但出现相同的错误。我也尝试过仅使用BasePresenter<Any?, Any?>
来解决直接问题,但是随后扩展了P: BasePresenter<Any?, Any?>
的所有内容给出一个错误,表明它正在期待P,但是得到了BasePresenter<Any?, Any?>
这是一个在我的代码中发生的示例
abstract class MvpRecyclerListAdapter<M, P : BasePresenter<Any?, Any?>, VH : MvpViewHolder<P>> : MvpRecyclerAdapter<M, P, VH>() {...}
在这一行上,我将在扩展
MvpRecyclerAdapter<M, P, VH>
的部分得到上面提到的错误我似乎无法解决这个问题。我该如何解决?
最佳答案
您已经在BasePresenter<out Any?, out Any?>
处将通用参数 V 声明为,在中声明了,因此presenter.bindView
一定不能使用输入参数。
解决方案:将声明更改为BasePresenter<out Any?, View?>
。
检查official doc以获得更多信息。