首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Mosby将演示程序附加到派生控件(视图)?

如何使用Mosby将演示程序附加到派生控件(视图)?
EN

Stack Overflow用户
提问于 2018-03-12 21:41:30
回答 1查看 64关注 0票数 1

否则,我在我的应用程序中成功地使用了Mosby。现在,我想在NavigationView控件中添加一个演示者。我已经重写了该控件,以封装视图式的东西,例如添加菜单项和动态显示子控件。

现在,我想将所有的演示代码从MainActivity.java移到NavigationDrawerPresenter类中,我想使用Mosby。

我已经阅读了Mosby文档,我没有看到它解释了如何将演示程序附加到我从SDK扩展到的视图控件,该控件位于活动布局的深处。我收集到,我可以直接使用ViewGroupMvpDelegateImpl,并手动将视图中的所有生命周期事件委托给委托。(这条路对吗?)

对于NavigationView来说,这是有问题的。

NavigationView继承自ScrimInsetsFrameLayout。它不允许我们覆盖onAttachedToWindowonDetachedFromWindow。它的答复是:

ScrimInsetsFrameLayout.onAttachedToWindow can only be called from within the same library group (groupId=com.android.support)

这似乎是莫斯比的表演障碍。如果没有这种覆盖,我不知道如何将Mosby的委托附加到生命周期事件中。

如何将演示者附加到从Android扩展的视图?

代码语言:javascript
复制
    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> {
        // ...
    }

}
EN

回答 1

Stack Overflow用户

发布于 2018-03-20 09:17:36

嗯,我也遇到过同样的问题。我想我们可以用

代码语言:javascript
复制
 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.
        }

    })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49244859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档