Android uses MVP mode (I)

I studied MVP before, but it didn't work for a long time. Today I watched it again. In fact, MVP is the upgraded version of MVC. MVP includes M-Model-model, V-View-view and C-Controller-controller.

Make a simple example, simple login. First, create a new project and create a layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Account login"
        android:textSize="18sp"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:textColor="#000"
        tools:ignore="HardcodedText" />

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        tools:ignore="LabelFor" />

    <EditText
        android:id="@+id/pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        tools:ignore="LabelFor" />

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sign in"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        tools:ignore="HardcodedText" />

</LinearLayout>

The layout is simple, just two Edittext and a login button. After that, I began to focus on.....

Layer V: it can be understood as activity or fragment.

Layer M: mainly deals with data operations, documents, network requests, etc.

P layer: processing data, connecting M and V layers through interfaces

The V layer already exists. Then, establish the M layer:


public class LoginModel {


    public String getLogin(String name, String pass){
        if (name.equals("123")&&pass.equals("123")){
            return "S";
        }else {
            return "F";
        }
    }

}

Yes, the M layer is so simple. It is to process the incoming account password and return a value correctly or wrongly. Then P layer:

package zj.it.bhne.mvp_demo;

public class LoginP {

    private LoginModel loginModel;
    private LoginView loginView;

    LoginP(LoginModel loginModel, LoginView loginView) {
        this.loginModel = loginModel;
        this.loginView = loginView;
    }

    public void setLogin(String name,String pass){
        this.loginView.loginView(this.loginModel.getLogin(name,pass));
    }

}

getLogin is an interface, which is the bridge used to connect layer M and layer V as mentioned earlier:

package zj.it.bhne.mvp_demo;

public interface LoginView {

    void loginView(String result);

}

In this way, the simplest MVP is built, and then the operation is performed in activity.

private LoginModel loginModel = new LoginModel();

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                LoginP loginP = new LoginP(loginModel);
                loginP.attachView(this);
                loginP.setLogin(name.getText().toString().trim(),pass.getText().toString().trim());
                break;
        }
    }


    @Override
    public void loginView(String result) {
        if (result.equals("S")){
            Toast.makeText(this, "Login successfully", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "Login failed", Toast.LENGTH_SHORT).show();
        }
    }

Note that to implement the interface you define in activity, you can define any name.

OK, that's it, but there are many bug s. It's just a simple implementation of mvp mode, and follow-up optimization.

Keywords: Android xml encoding Fragment

Added by Stickdragon on Tue, 31 Dec 2019 06:19:49 +0200