4

我已经定义了一个这样的类

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)
    }
}

wherepresenter.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>

我似乎无法解决这个问题。我该如何解决?

4

1 回答 1

5

您已经为泛型参数V at声明了 out,因此不得采用输入参数。BasePresenter<out Any?, out Any?>presenter.bindView

解决方案:将声明更改为BasePresenter<out Any?, View?>.

查看官方文档以获取更多信息。

于 2017-11-02T01:44:31.263 回答