具有多个片段的活动的MVP [英] MVP for Activity with multiple Fragments

查看:89
本文介绍了具有多个片段的活动的MVP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个片段的活动。

I have an Activity with two Fragments in it.

活动( MainActivity )从中检索数据开放的天气api。我为此实现了MVP,其中:
Model 包含来自API的所有响应对象

View 活动

Presenter 包含 MainPresenter MainPresenterImpl MainView GetDataInteractor GetDataInteractorImpl

The activity (MainActivity) retrieves data from an open weather api. I have implemented MVP for this in which: Model contains all the response objects from the API
View is the Activity
Presenter contains MainPresenter, MainPresenterImpl, MainView, GetDataInteractor and GetDataInteractorImpl.

因此,活动从Web服务获取数据。两个片段都将显示活动中检索到的数据中的数据。

So, the activity gets the data from the web service. Both fragments will display data from the data retrieved in the activity.

在这种情况下使用MVP的最佳做法是什么?我知道如何通过接口/回调在片段< - >活动之间传递数据,我的问题是在实现MVP时这种行为会改变吗?

What is the best practice using MVP in this situation? I know how to pass data between fragments <-> activity via interface/callbacks, my question is does this behaviour change when implementing MVP?

推荐答案

活动/片段应该被视为MVP模型中的视图。这意味着他们应该只显示数据并接收用户交互。
可以通过接口/回调来传递活动和片段。

The activity/fragments should be considered as just the view in the MVP model. This means that they should just show data and receive user interactions. It is ok to communicate activity and fragments via interface/callbacks.

但是,调用API服务不是活动/片段的责任。

But, it is not an activity/fragment responsibility to call the API services.

演示者应负责调用api服务。

The presenter should be responsible to call the api services.

因此,演示者应公开类似<$的方法c $ c> loadXXX ,在内部它会调用该服务。收到回复后,演示者应使用服务结果调用 view.showXXX 。活动/片段应调用此 loadXXX 方法并实现 showXXX

So, the presenter should expose a method like loadXXX, internally it would make the call to the service. When the response is received, the presenter should call view.showXXX with the results of the service. The activity/fragment should call this loadXXX method and implement the showXXX.

通常,会在活动/片段中创建或注入演示者。
活动/片段必须实现由演示者公开的接口,并且演示者持有该接口的弱引用,以便它可以回调。

Usually, the presenter is created or injected into the activity/fragment. The activity/fragment has to implement an interface exposed by the presenter, and the presenter holds a weak reference of this interface, so that it can callback.

当用户与屏幕交互时,例如按钮上的 onClick ,活动/片段调用演示者中的相应方法,例如 presenter.loadUserDetails()演示者告诉视图显示为加载,例如 view.showAsLoading()因为它必须做它的东西:可能验证某些东西或从api服务加载数据,最后回调结果到视图,例如 view.showUserDetails(userDetails)

When the user interacts with the screen, for example, an onClick on a button, the activity/fragment calls the corresponding method in the presenter, e.g. presenter.loadUserDetails() the presenter tells the view to show as loading, e.g. view.showAsLoading() because it has to do its stuff: maybe validating something or loading data from an api service and finally callback with the results to the view, e.g. view.showUserDetails(userDetails).

总结一个例子,在MVP的各个部分的代码中:

To summarize, an example, in code of the various parts of MVP:

活动/片段仅代表MVP视图:

Activity/Fragment represents just the View of MVP:

public class MyActivity extends AppCompatActivity implements MyPresenter.View {
    private MyPresenter mPresenter;

    public onCreate() {
        ...
        mPresenter = new MyPresenter(this); // Or inject it and then set the view.
    }

    public void onClick(View v) {
        mPresenter.loadXXX(param1, param2);
    }

    // MyPresenter.View methods

    public void showAsLoading() {
        ...
    }

    public void showUserDetails(UserDetails userDetails) {
        ...
    }
}

型号:

public class UserDetails {
    ...
}

演示者:

public class MyPresenter {

    private WeakReference<MyPresenter.View> mWeakView;

    public MyPresenter(MyPresenter.View view) {
        mWeakView = new WeakReference(view);
    }

    public void loadXXX(String param1, String param2) {
        MyPresenter.View view = mWeakView.get();
        if (view != null) {
            view.showAsLoading();
            // Do stuff, e.g. make the Api call and finally call view.showUserDetails(userDetails);
        }
    }

    interface View {
        void showAsLoading();
        void showUserDetails(UserDetails userDetails);
    }

}

这篇关于具有多个片段的活动的MVP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆