【问题标题】:How to attach a presenter to a derived control (view) using Mosby?如何使用 Mosby 将演示者附加到派生控件(视图)?
【发布时间】:2018-03-12 21:41:30
【问题描述】:

我在我的应用程序中使用 Mosby 是成功的。我现在想将演示者添加到NavigationView 控件。我已经重写了控件以封装类似于视图的内容,例如添加菜单项和动态显示的子控件。

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

我已通读 Mosby 文档,但没有看到其中解释了如何将演示者附加到我从位于 Activity 布局深处的 SDK 扩展的 View 控件。我收集到我可以直接使用 ViewGroupMvpDelegateImpl 并手动将所有生命周期事件从视图委托给委托。 (这是正确的方法吗?)

对于NavigationView,这是有问题的。

NavigationView 继承自 ScrimInsetsFrameLayout。它不允许我们覆盖onAttachedToWindowonDetachedFromWindow。它回应:

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

这似乎是 Mosby 的一大亮点。如果没有这个覆盖,我不知道如何将 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> {
        // ...
    }

}

【问题讨论】:

  • 关于这个主题的任何更新? IMO,隐藏像onAttachedToWindowonDetachedFromWindow这样的全局钩子是愚蠢的。
  • 通常您只需将onAttachedToWindow()onDetachedFromWindow() 委托给ViewGroupDelegate()。由于这些方法是最终的,您必须使用addOnAttachStateChangeListener() 作为@guness 指出的解决方法(请参阅下面的答案)

标签: android mosby


【解决方案1】:

嗯,我也遇到了同样的问题。我认为我们可以使用

 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.
        }

    })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    相关资源
    最近更新 更多