文章目录
  1. 1. Android MVP 实例
    1. 1.1. Android MVP 实例演示
  2. 2. Android MVP详解
    1. 2.1. MVP model
    2. 2.2. MVP presenter
    3. 2.3. MVP View
    4. 2.4. bean

Android MVP 实例

Android MVP 实例演示

效果展示

Android MVP详解

MVP 流程图

说明:
步骤1:View引用Presenter
步骤2:Presenter调用Model,走Model具体逻辑
步骤3:Model逻辑实现,回调Presenter方法
步骤4:Presenter回调View

目录图

MVP model

IStudentModel.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public interface IStudentModel {

/**
* 保存学生信息
* @param id 学号
* @param name 姓名
* @param sex 性别
*/
void save(int id,String name,String sex);

/**
* 读取学生信息
* @param id 学号
* @return
*/
Student load(int id);
}

StudentModel.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class StudentModel implements IStudentModel {

private int id;
private String name;
private String sex;
private SparseArray<Student> array = new SparseArray<>();


@Override
public void save(int id,String name,String sex) {
Student student = new Student();
student.setId(id);
student.setName(name);
student.setSex(sex);
array.append(id, student);
}

@Override
public Student load(int id) {
Student notFound = new Student();
notFound.setName("没有发现");
notFound.setSex("没有发现");
Student student=array.get(id, notFound);
return student;
}
}

MVP presenter

UserPresenter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class UserPresenter {
private IStudentModel iStudentModel;
private IMainView iMainView;

public UserPresenter(IMainView iMainView) {
this.iMainView = iMainView;
iStudentModel = new StudentModel();
}

public void save(int id, String name, String sex) {
iStudentModel.save(id,name,sex);
}

public void load(int id) {
Student student=iStudentModel.load(id);
iMainView.setName(student.getName());
iMainView.setSex(student.getSex());
}
}

MVP View

IMainView.java

1
2
3
4
5
6
7
8
9
10
11
12
public interface IMainView {

void setName(String name);

void setSex(String sex);

int getId();

String getName();

String getSex();
}

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public class MainActivity extends Activity implements View.OnClickListener, IMainView {

private EditText et_id;
private EditText et_name;
private EditText et_sex;
private Button bt_save;
private Button bt_load;

private UserPresenter userPresenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

initView();
}

private void initView() {
et_id = (EditText) findViewById(R.id.et_id);
et_name = (EditText) findViewById(R.id.et_name);
et_sex = (EditText) findViewById(R.id.et_sex);
bt_save = (Button) findViewById(R.id.bt_save);
bt_load = (Button) findViewById(R.id.bt_load);

bt_save.setOnClickListener(this);
bt_load.setOnClickListener(this);

userPresenter =new UserPresenter(this);
}


@Override
public void onClick(View v) {

switch (v.getId()) {
case R.id.bt_save:
userPresenter.save(getId(),getName(),getSex());
break;
case R.id.bt_load:
userPresenter.load(getId());
break;
default:
break;
}
}

@Override
public void setName(String name) {
et_name.setText(name);
}

@Override
public void setSex(String sex) {
et_sex.setText(sex);
}

@Override
public int getId() {
return Integer.parseInt(et_id.getText().toString());
}

@Override
public String getName() {
return et_name.getText().toString();
}

@Override
public String getSex() {
return et_sex.getText().toString();
}
}

bean

Student.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Student {

private int id;
private String name;
private String sex;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}
}

以上就是AndroidMVP的一个小demo,以上的源码我已经开源在Github,地址:AndroidMVP

欢迎大家关注我的微信公众号,我会不定期的分享些Android开发的技巧

文章目录
  1. 1. Android MVP 实例
    1. 1.1. Android MVP 实例演示
  2. 2. Android MVP详解
    1. 2.1. MVP model
    2. 2.2. MVP presenter
    3. 2.3. MVP View
    4. 2.4. bean