3

我正在尝试在 Dagger 2 中使用 Mosby 的 MvpBasePresenter 设置 Base Presenter 我有以下基本组件:

public interface BaseViewComponent<V extends BaseView, P extends BasePresenter> {
    void injectView(V object);

    void injectPresenter(P object);

    P getPresenter();
}

使用以下 BasePresenter

public class BasePresenter <V extends BaseView> extends MvpBasePresenter {
    @Inject public BasePresenter () {

    }
}

和以下 BaseActivity

public abstract class BaseActivity<P extends BasePresenter, C extends BaseViewComponent> extends MvpActivity implements BaseView<P> {

@Inject C mComponent;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getViewContentId() != 0) {
        setContentView(getViewContentId());
        ButterKnife.bind(this);
    }

}

@Override
public P createPresenter() {
    return mComponent.getPresenter();
}
}

出于某种原因,即使组件 getPresenter 返回 P,创建演示者也显示为返回类型 BasePresenter 而不是类型 P。有人有任何见解吗?这是一个好主意吗?

4

1 回答 1

0

我最初的猜测如下,与此类似

public interface BaseViewComponent<V extends BaseView<V, P>, P extends BasePresenter<V, P>> {
    void injectView(V object);

    void injectPresenter(P object);

    P getPresenter();
}

public class BasePresenter<V extends BaseView<V, P>, P extends BasePresenter<V, P>> extends MvpBasePresenter {
    @Inject public BasePresenter () {

    }
}

public abstract class BaseActivity<V extends BaseView<V, P>, P extends BasePresenter<V, P>, C extends BaseViewComponent<V, P>> 
    extends MvpActivity 
    implements BaseView<V, P> {

@Inject C mComponent;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getViewContentId() != 0) {
        setContentView(getViewContentId());
        ButterKnife.bind(this);
    }

}

@Override
public P createPresenter() {
    return mComponent.getPresenter();
}
}
于 2015-12-17T13:11:28.400 回答