我在我的应用程序中成功使用Mosby。我现在正处于向NavigationView
控件添加演示者的位置。我已经重写了控件以封装视图等内容,例如添加菜单项和动态显示的子控件。
我现在想将所有主持人代码从MainActivity.java
移到NavigationDrawerPresenter
类中,我想使用Mosby。
我已经阅读了Mosby文档,但我没有看到它解释如何将演示者附加到View控件的位置,该控件是我从位于Activity布局深处的SDK扩展而来的。我收集到我可以直接使用ViewGroupMvpDelegateImpl并手动将所有生命周期事件从视图委托给委托。 (这是正确的方法吗?)
对于NavigationView
,这是有问题的。
NavigationView
继承自ScrimInsetsFrameLayout
。它不允许我们覆盖onAttachedToWindow
或onDetachedFromWindow
。它回应:
ScrimInsetsFrameLayout.onAttachedToWindow can only be called from within the same library group (groupId=com.android.support)
这似乎是莫斯比的一个表现。如果没有这种覆盖,我不知道如何将Mosby的委托附加到生命周期事件中。
如何将演示者附加到我从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> {
// ...
}
}
答案 0 :(得分:0)
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.
}
})