gpt4 book ai didi

android - 使用 Mosby 实现非 ViewGroup MVP Android View

转载 作者:行者123 更新时间:2023-11-30 00:13:36 25 4
gpt4 key购买 nike

我正在尝试使用 Mosby 实现一个 MVP Android View(不是 Activity 或 fragment ),但是当在 Android Adaptor 中使用该 View 并在 onBindViewHolder 中访问它时,演示者此时未初始化。似乎在 onBindViewHolder 完成之后才调用 onAttachWindow,因为演示者为 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 实现。我过去使用过另一个 MVP 库,称为 Moxy,它具有用于 onCreate 和 onAttachToWindow 的委托(delegate)方法。

我可以添加一个在构造函数中调用 getMvpDelegate().onAttachWindow 的初始化例程,但这看起来更像是一个 hack,而不是其他任何东西。关于在 onBindViewHolder 和 RecyclerView 中使用时如何使它工作的任何想法?

最佳答案

不要在您的适配器中使用 MVP。它只会让事情变得复杂和繁琐。

例子:Views/ViewHolder 可以回收,Presenter 也应该回收吗?如果是,您如何实现并确保内部 Presenter 是最新的?如何在滚动时重新附加 Presenter 以查看?如果您的 AdapterSet 被更改怎么办?屏幕方向发生变化怎么办?

千万不要那样做。

关于android - 使用 Mosby 实现非 ViewGroup MVP Android View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47715948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com