简体   繁体   English

Android MVP:在片段中定义Presenter

[英]Android MVP: defining Presenter in Fragments

I have done lot of search on Google and also on Stackoverflow but still I'm confused, so asking a question here. 我在谷歌和Stackoverflow上做了很多搜索,但我仍然很困惑,所以在这里问一个问题。

I have this small MVP design pattern - 我有这个小MVP设计模式 -

SigninView.java SigninView.java

public interface SigninView{

    void onSuccess();
    void onError();
    void onComplete();
}

SigninPresenter.java SigninPresenter.java

public interface SigninPresenter {

    void signIn(String emailID, String password);
}

SigninModel.java SigninModel.java

public class SigninModel implements SigninPresenter {

    private SigninView mSigninView;

    public SigninModel(SigninView mSigninView) {
        this.mSigninView = mSigninView;
    }

    @Override
    public void signIn(String emailID, String password) {

        if(emailID.equals("abc@example.com") && password.equals("123")){
            mSigninView.onSuccess();
        }
        else{
            mSigninView.onError();
        }

        mSigninView.onComplete();

    }
}

I want to implement the SigninView on a Fragment and define the SigninPresenter there itself like this - 我想在片段上实现SigninView并在其中定义SigninPresenter ,就像这样 -

SigninPresenter mSigninPresenter = new SigninModel(view_of_mvp);
mSigninPresenter.signIn("adadada", "asads");

See one reference here. 请参阅此处的一个参考 I want to implement a View and define a Presenter like this but on a Fragment - https://github.com/ashokslsk/Comprehensive-MVP/blob/master/app/src/main/java/com/ashokslsk/mvpexample/MainActivity.java 我想实现一个View并定义一个这样的Presenter但是在片段上 - https://github.com/ashokslsk/Comprehensive-MVP/blob/master/app/src/main/java/com/ashokslsk/mvpexample/MainActivity的.java

How to achieve that ? 怎么实现呢?

You don't actually need to pass the context, but rather the implementation of your SigninView. 您实际上不需要传递上下文,而是需要传递SigninView。 So you need to make your fragment implement SigninView 所以你需要让你的片段实现SigninView

MyFragment implements SigninView

and simply initialize the presenter with this , instead of context. 并简单地用this而不是上下文来初始化演示者。 In fact, you presenter shouldn't know much about the Android SDK, so it shouldn't deal with contexts. 实际上,您的演示者不应该对Android SDK了解多少,因此它不应该处理上下文。 See this answer . 看到这个答案

SigninPresenter mSigninPresenter = new SigninModel(this);

EDIT: 编辑:

You had the activity like this: 你有这样的活动:

public class MainActivity extends AppCompatActivity implements SigninView 

All you have to do is make your fragment implement SigninView: 您所要做的就是让您的片段实现SigninView:

public class MyFragment extends Fragment implements SigninView 

And then, in onCreateView you can initialize the presenter like this: 然后,在onCreateView中,您可以像这样初始化演示者:

signinPresenter = new SigninPresenterImpl(this);

Your View already implements getContext() . 您的View已经实现了getContext() Just add it in your interface and call it in your SigninModel : 只需将其添加到您的界面中并在SigninModel调用它:

SigninPresenter.java SigninPresenter.java

public interface SigninView {

    void onSuccess();
    void onError();
    void onComplete();

    Context getContext();
}

SigninModel.java SigninModel.java

mSigninView.getContext();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM