Android MVP模式简单例子实战

来源:互联网 发布:mac照片文件夹在哪里 编辑:程序博客网 时间:2024/04/28 00:44

概要

  • 对于mvp模式,大家都知道是由mvc演变而来的,对于MVC大家都知道

    • M Model(用于存放实体模型与业务逻辑)
    • V View(存放布局和资源文件)
    • C Control(存放Activity)
  • 对于mvc中的view在Android中的功能就比较弱化了并且在Control中的activity中我们在实际开发中经常都是上千行代码,经常都是将对一些点击时间的处理和一些逻辑都放在activity中,这样就造成了activity很臃肿,并且这样对于activity在mvc中处于View与Control之间 这个尴尬的位置

  • 对于mvp模式的出现则挽回了activity在mvc中的尴尬局面,mvp主要为

    • M Model(存放数据模型与业务逻辑)
    • V View(存放activity 已经一些自定义的View)
    • P Presenter(主要负责View与Model的交互)
  • 我们可以看到mvc由mvp的转化主要将Control换成了Presenter,由Presenter来处理activity中的逻辑和与Model层的交互

总结

其实对于mvc转换到mvp我们可以用下面的两张图来解释一下

mvc模式

图片来源网络,侵权删

mvp模式

图片来源网络,侵权删


我们可以看出上面的两张图,由activity直接向Model层通信,转化到 View层通过Presenter层向 Model的数据类型通信,这样可以明显的看出很大程度降低了程序的耦合度,废话也不多说了,下面我们用一个提交信息的例子来让大家更直观的理解mvp

实战

下面我用一个简单的提交信息的例子来说明下mvp在这个例子是怎么用的,下面是示例图

这里写图片描述

下面是项目的整个结构


我们可以看出我们在MainActivity中的一些逻辑性的代码全都通过MainActiityPresenterCompl来实现,由他直接向Model层通信

下面是代码

信息模型

/** * Created by blue on 2016/10/15. */public class UserInfo {    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getGender() {        return gender;    }    public void setGender(String gender) {        this.gender = gender;    }    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }    public String getHobby() {        return hobby;    }    public void setHobby(String hobby) {        this.hobby = hobby;    }    private String age;    private String name;    private String gender;    private String hobby;}
  • 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
  • 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

presenter的接口

package com.mvpdemo.blue.mvpdemo.presenter.ipresenter;import android.content.Context;import android.widget.EditText;import android.widget.ProgressBar;import java.util.List;/** * Created by blue on 2016/10/15. */public interface IMainActivityPresenter {    void submitData(Context context , List<EditText> editList, ProgressBar progressBar);    void initData(List<EditText> editList);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

mainActivity的presenter具体实现类

import android.content.Context;import android.os.Handler;import android.os.Looper;import android.view.View;import android.widget.EditText;import android.widget.ProgressBar;import android.widget.Toast;import com.mvpdemo.blue.mvpdemo.model.info.UserInfo;import com.mvpdemo.blue.mvpdemo.presenter.ipresenter.IMainActivityPresenter;import java.util.List;/** * Created by blue on 2016/10/15. */public class MainActivityPresenterCompl implements IMainActivityPresenter {    @Override    public void submitData(final Context context, final List<EditText> editList, final ProgressBar progressBar) {        progressBar.setVisibility(View.VISIBLE);        final Handler mainHandler = new Handler(Looper.getMainLooper());        new Thread() {            @Override            public void run() {                /*模拟提交信息*/                try {                    Thread.sleep(2000);                } catch (InterruptedException e) {                    e.printStackTrace();                }                UserInfo info = new UserInfo();                info.setAge(editList.get(0).getText().toString());                info.setGender(editList.get(1).getText().toString());                info.setName(editList.get(2).getText().toString());                info.setHobby(editList.get(3).getText().toString());                mainHandler.post(new Runnable() {                    @Override                    public void run() {                        progressBar.setVisibility(View.INVISIBLE);                        Toast.makeText(context,"提交数据完成",Toast.LENGTH_SHORT).show();                        initData(editList);                    }                });            }        }.start();    }    @Override    public void initData(List<EditText> editList) {        for (EditText editText:editList)            editText.setText("");    }}
  • 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
  • 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

MainAcitivty文件与接口

package com.mvpdemo.blue.mvpdemo.view.activity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ProgressBar;import com.mvpdemo.blue.mvpdemo.R;import com.mvpdemo.blue.mvpdemo.presenter.compl.MainActivityPresenterCompl;import com.mvpdemo.blue.mvpdemo.presenter.ipresenter.IMainActivityPresenter;import com.mvpdemo.blue.mvpdemo.view.interfaceview.IMainActivityView;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity implements IMainActivityView, View.OnClickListener {    private IMainActivityPresenter mainActivityPresenter ;    private List<EditText> editList ;    private ProgressBar progressBar ;    private Button btn_submit;    private Button btn_clean;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        init();        initView();    }    @Override    public void init() {        mainActivityPresenter = new MainActivityPresenterCompl();        editList = new ArrayList<>();    }    @Override    public void initView() {        editList.add((EditText) findViewById(R.id.age));        editList.add((EditText) findViewById(R.id.gender));        editList.add((EditText) findViewById(R.id.name));        editList.add((EditText) findViewById(R.id.hobby));        progressBar = (ProgressBar) findViewById(R.id.submit_pro);        btn_clean = (Button) findViewById(R.id.clean);        btn_submit = (Button) findViewById(R.id.submit);        btn_submit.setOnClickListener(this);        btn_clean.setOnClickListener(this);        progressBar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));    }    @Override    public void submitData() {        mainActivityPresenter.submitData(this,editList,progressBar);    }    @Override    public void initData() {        mainActivityPresenter.initData(editList);    }    @Override    public void onClick(View view) {        switch (view.getId()) {            case R.id.submit:                submitData();                break;            case R.id.clean:                initData();                break;        }    }}
  • 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
  • 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
package com.mvpdemo.blue.mvpdemo.view.interfaceview;/** * Created by blue on 2016/10/15. */public interface IMainActivityView {    void initView();    void submitData();    void initData();    void init();}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

下面是布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.mvpdemo.blue.mvpdemo.view.activity.MainActivity">    <EditText        android:id="@+id/age"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="年龄"        />    <EditText        android:id="@+id/gender"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="性别"        />    <EditText        android:id="@+id/name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="名字"        />    <EditText        android:id="@+id/hobby"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="爱好"        />    <LinearLayout        android:layout_width="match_parent"        android:paddingEnd="30dp"        android:layout_marginBottom="50dp"        android:layout_height="wrap_content">        <Button            android:id="@+id/submit"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:layout_marginEnd="30.0dp"            android:layout_marginRight="30.0dp"            android:text="提交"            />        <Button            android:id="@+id/clean"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginRight="30.0dp"            android:layout_marginLeft="40.0dp"            android:text="清除"/>    </LinearLayout>    <ProgressBar        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:visibility="invisible"        android:layout_gravity="center_horizontal"        android:layout_marginBottom="50dp"        android:id="@+id/submit_pro"        /></LinearLayout>
  • 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
  • 73
  • 74
  • 75
  • 76
  • 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
  • 73
  • 74
  • 75
  • 76

这就是mvp模式这也只是一个最简单的例子,也只能用来理解mvp,更高深的运用,还需要继续学习,就到这里了,需要源码的朋友,请在下面留言

0 0
原创粉丝点击