[英]How to attach a presenter to a derived control (view) using Mosby?
I'm otherwise successful using Mosby in my app. 否则,我可以在我的应用程序中成功使用Mosby。 I'm now at the point where I'd like to add a presenter to a
NavigationView
control. 现在,我想将演示者添加到
NavigationView
控件中。 I've overridden the control to encapsulate view-ish things such as adding menu items and dynamically appearing child controls. 我已经重写了控件,以封装诸如添加菜单项和动态显示子控件之类的视图风格的东西。
I would now like to move all of the presenter-ish code from the MainActivity.java
into a NavigationDrawerPresenter
class and I'd like to use Mosby. 现在,我想将所有演示者代码从
MainActivity.java
移到NavigationDrawerPresenter
类中,并且我想使用Mosby。
I've read through the Mosby docs and I didn't see where it explains how to attach a presenter to a View control that I've extended from the SDK that is sitting deep inside of a Activity layout. 我已经阅读了Mosby文档,但没有看到它解释了如何将演示者附加到我已经从位于Activity布局内部的SDK扩展的View控件上的地方。 I gathered that I could use the ViewGroupMvpDelegateImpl directly and manually delegate all of the lifecycle events from the view to the delegate.
我收集到可以直接使用ViewGroupMvpDelegateImpl并手动将视图中的所有生命周期事件委托给委托。 (Is this the right way?)
(这是正确的方法吗?)
In the case of the NavigationView
, this is problematic. 在
NavigationView
的情况下,这是有问题的。
NavigationView
inherits from ScrimInsetsFrameLayout
. NavigationView
继承自ScrimInsetsFrameLayout
。 It doesn't let us override onAttachedToWindow
or onDetachedFromWindow
. 它不允许我们重写
onAttachedToWindow
或onDetachedFromWindow
。 It responds with: 它响应:
ScrimInsetsFrameLayout.onAttachedToWindow can only be called from within the same library group (groupId=com.android.support)
This seems to be a showstopper for Mosby. 这似乎是Mosby的亮点。 Without this override, I don't know how to attach Mosby's delegate to the lifecycle events.
没有这种覆盖,我不知道如何将Mosby的委托附加到生命周期事件。
How do I attach a presenter to a view that I have extended from the Android SDK? 如何将演示者附加到从Android SDK扩展的视图中?
public class NavigationDrawerView extends NavigationView
implements NavigationDrawerContract.View,
ViewGroupDelegateCallback<NavigationDrawerContract.View, NavigationDrawerContract.Presenter>, MvpView,
NavigationView.OnNavigationItemSelectedListener {
protected ViewGroupMvpDelegate<NavigationDrawerContract.View, NavigationDrawerContract.Presenter> mvpDelegate;
protected NavigationDrawerContract.Presenter presenter;
//...
public NavigationDrawerView(Context context) {
super(context);
}
public NavigationDrawerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NavigationDrawerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//...
@NonNull
protected ViewGroupMvpDelegate<NavigationDrawerContract.View, NavigationDrawerContract.Presenter> getMvpDelegate() {
if (mvpDelegate == null) {
mvpDelegate = new ViewGroupMvpDelegateImpl<>(this, this, true);
}
return mvpDelegate;
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
getMvpDelegate().onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
getMvpDelegate().onDetachedFromWindow();
}
//...
}
public class NavigationDrawerPresenter
extends MvpNullObjectBasePresenter<NavigationDrawerContract.View>
implements NavigationDrawerContract.Presenter {
//...
}
public interface NavigationDrawerContract {
interface View extends MvpView {
// ...
}
interface Presenter extends MvpPresenter<View> {
// ...
}
}
Hmm, I have faced same problem. 嗯,我也遇到过同样的问题。 I think we can use
我认为我们可以使用
addOnAttachStateChangeListener(object : OnAttachStateChangeListener {
override fun onViewDetachedFromWindow(v: View?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onViewAttachedToWindow(v: View?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.