否则,我在我的应用程序中成功地使用了Mosby。现在,我想在NavigationView控件中添加一个演示者。我已经重写了该控件,以封装视图式的东西,例如添加菜单项和动态显示子控件。
现在,我想将所有的演示代码从MainActivity.java移到NavigationDrawerPresenter类中,我想使用Mosby。
我已经阅读了Mosby文档,我没有看到它解释了如何将演示程序附加到我从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扩展的视图?
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> {
// ...
}
}发布于 2018-03-20 09:17:36
嗯,我也遇到过同样的问题。我想我们可以用
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.
}
})https://stackoverflow.com/questions/49244859
复制相似问题