我试图用Mosby实现MVP Android View(不是活动或片段),但是当在Android适配器中使用视图并在onBindViewHolder中访问它时,此时不会初始化演示者。看起来onAttachWindow直到onBindViewHolder完成后才被调用,因为演示者是Null。这是我创建的抽象类。
public abstract class MvpImageView<V extends MvpView, P extends MvpPresenter<V>>
extends AppCompatImageView implements MvpView, ViewGroupDelegateCallback<V, P> {
protected P presenter;
protected ViewGroupMvpDelegate<V, P> mvpDelegate;
private boolean retainInstance = false;
public MvpImageView(Context context) {
super(context);
}
public MvpImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MvpImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* Get the mvp delegate. This is internally used for creating presenter, attaching and detaching
* view from presenter etc.
*
* <p><b>Please note that only one instance of mvp delegate should be used per android.view.View
* instance</b>.
* </p>
*
* <p>
* Only override this method if you really know what you are doing.
* </p>
*
* @return {@link ViewGroupMvpDelegate}
*/
@NonNull protected ViewGroupMvpDelegate<V, P> getMvpDelegate() {
if (mvpDelegate == null) {
mvpDelegate = new ViewGroupMvpDelegateImpl<>(this, this, true);
}
return mvpDelegate;
}
@Override protected void onAttachedToWindow() {
super.onAttachedToWindow();
Log.d(getClass().getName(), "Attaching to Window");
getMvpDelegate().onAttachedToWindow();
}
@Override protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
Log.d(getClass().getName(), "Detaching from Window");
getMvpDelegate().onDetachedFromWindow();
}
@SuppressLint("MissingSuperCall") @Override protected Parcelable onSaveInstanceState() {
return getMvpDelegate().onSaveInstanceState();
}
@SuppressLint("MissingSuperCall") @Override
protected void onRestoreInstanceState(Parcelable state) {
getMvpDelegate().onRestoreInstanceState(state);
}
/**
* Instantiate a presenter instance
*
* @return The {@link MvpPresenter} for this view
*/
public abstract P createPresenter();
@Override public P getPresenter() {
return presenter;
}
@Override public void setPresenter(P presenter) {
this.presenter = presenter;
}
@Override public V getMvpView() {
return (V) this;
}
@Override public final Parcelable superOnSaveInstanceState() {
return super.onSaveInstanceState();
}
@Override public final void superOnRestoreInstanceState(Parcelable state) {
super.onRestoreInstanceState(state);
}
}
这是基于MvpLinearLayout实现。我过去使用过另一个名为Moxy的MVP库,它有onCreate和onAttachToWindow的委托方法。
我可以在构造函数中添加一个调用getMvpDelegate()。onAttachWindow的初始化例程,但这看起来更像是一个hack而不是其他任何东西。在onBindViewHolder和RecyclerView中使用时如何使其工作的任何想法?
答案 0 :(得分:1)
不要在适配器中使用MVP。它只会使事情变得复杂和繁琐。
示例:如果Presenter也可以回收,可以回收Views / ViewHolder吗?如果是,您如何实现这一点并确保内部Presenter是新鲜的?如何在滚动时重新连接Presenter进行查看?如果您的AdapterSet发生了变化怎么办?屏幕方向改变怎么样?
根本不要那样做。