Simple example of Android MVP mode

Keywords: network Android Fragment

MVP mode is M (Model), V (View) and P (Presenter)

Model is data. How to obtain data and data type belong to model. In a word, all data related classes and operations belong to model

View is the interface displayed to the user, including all kinds of controls and click events. In Android, Activity and Fragment belong to view

Presenter is a mediation. View does not directly manipulate data, but indirectly manipulates data by calling presenter.

 

Here is an example:

First of all, each MVP needs to define the corresponding interface to describe the functions to be implemented, and then it needs to implement the corresponding interface.

 

The overall idea is that Network, MainActivity and PresenterComl need to implement corresponding interfaces

In PresenterComl, there are instances of Network objects and IView interfaces. In the PresenterComl request network interface method, the Network request network interface method is invoked, and then IView interface is implemented through IView to enable callbacks to transmit data to MainActivity.

 

Network:

public class Network implements IModel{


    @Override
    public void request(String url,Callback callback) {
        OkHttpClient okHttpClient=new OkHttpClient.Builder().build();
        final Request request=new Request.Builder().url(url).build();
        okHttpClient.newCall(request).enqueue(callback);
    }
}

 

PresenterComl:

public class PresenterComl implements IPresenter {
    Network network;
    IView view;
    Handler handler = new Handler(Looper.getMainLooper());
    public PresenterComl(IView view) {
        network = new Network();
        this.view = view;
    }
    @Override
    public void requestData(String url) {
        network.request(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onFailed();
                    }
                });
            }
            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                final String s = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onSuccess(s);
                    }
                });
            }
        });
    }
}

 

MainActivity:

public class MainActivity extends AppCompatActivity implements IView, View.OnClickListener {

    private Button mBtn;
    private TextView mTxt;
    private IPresenter mPresent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBtn = findViewById(R.id.btn);
        mTxt = findViewById(R.id.txt);
        mBtn.setOnClickListener(this);

        mPresent = new PresenterComl(this);
    }
    @Override
    public void onSuccess(String message) {
        mTxt.setText("Message received successfully:\n" + message);
    }
    @Override
    public void onFailed() {
        mTxt.setText("Failure to receive");
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn) {
            mPresent.requestData("https:www.baidu.com");
        }
    }
}

 

Posted by lajkonik86 on Tue, 17 Dec 2019 08:10:34 -0800