当前位置: 萬仟网 > IT编程>移动>Android > Android设计模式——MVP

Android设计模式——MVP

2019年05月08日 07:39  | 萬仟网IT编程  | 我要评论

一、什么是mvp

mvp:全称 model-view-presenter。

mvp框架由3部分组成:view层负责显示,presenter层负责逻辑处理,model层提供数据。

view:负责绘制ui元素、与用户进行交互(在android中体现为activity,fragment)

model:负责存储、检索、操纵数据

presenter:作为view与model交互的中间纽带,处理与用户交互的负责逻辑。
image.png

二、创建model presenter view三个包

image.png

三、代码编写

1.view层代码

view层负责界面操作,如何确定view层里面有那些操作呢?
拿我这个userview举例.
onlogin代表登录时候要做的事情,比如显示一个进度dialog
loginsuccess代表登录成功后要做的事情,做界面跳转等操作
loginfail表示登录失败后得操作,显示一个toast等

public interface  userview{
    void onlogin();
    void loginsuccess(userbean bean);
    void loginfail(exception e,int code);
}

2.presenter层代码

这层主要负责view层和model层的连接,也有中间人得意思,
view层请求presenter层,presenter在请求model层,model层处理数据之后通过回调接口传递给presenter层,presenter在传递给view层

在上一张图
image.png

public class userpresenter implements usercallback {
    private userview  muserview;
    private usermodel mmodel;

    public userpresenter(userview baseview) {
        muserview = baseview;
        mmodel= new usermodel(this);
    }

    public void userlogin(string name, string pass){
        muserview.onlogin();
        mmodel.userlogin(name,pass);
    }

    @override
    public void onsuccess(userbean bean) {
        muserview.loginsuccess(bean);
    }

    @override
    public void onfail(exception e, int code) {
        muserview.loginfail(e,code);

    }
}
public  interface  usercallback {
    void onsuccess(userbean bean);
    void onfail(exception e ,int code);
}

3.model层代码

model层进行网络请求等耗时任务处理,model会留有一个回调地址,当数据处理完成之后回调它,在本实例中usercallback就是一个回调接口,具体实现类在presenter层,也就是说数据处理完成之后会传递给presenter层

public class usermodel {
    private usercallback musercallback;
    private map<string,string> musermaps;
    private handler mhandler;

    /**
     * 
     * @param usercallback 回调地址,当数据处理完成之后调用这个接口
     */
    public usermodel(usercallback usercallback) {
        musercallback = usercallback;
        musermaps=new hashmap<>();
        mhandler =new handler(looper.getmainlooper());
        musermaps.put("user1","123456");
        musermaps.put("user2","12345");
        musermaps.put("user3","1234");

    }

    public void userlogin(final  string name, final string pass){
        /**
         * 模拟延时操作,2秒后调用usercallback接口,回传数据
         */
       mhandler.postdelayed(new runnable() {
           @override
           public void run() {
               if (musermaps.containskey(name) && musermaps.get(name).equals(pass)){
                   userbean bean =new userbean(name,pass,20);
                   musercallback.onsuccess(bean);
               }else {
                   exception exception =new exception("用户名或账号错误");
                   musercallback.onfail(exception,400);
               }
           }
       },2000);
    }

}

4.mainactivity

public class mainactivity extends appcompatactivity implements userview ,view.onclicklistener {
    private  progressdialog mprogressdialog;
    private edittext musertext;
    private edittext muserpass;
    private button mloginbtn;
    private userpresenter muserpresenter;
    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);
        muserpresenter=new userpresenter(this);
        initview();
        initevents();
    }

    private void initevents() {
        mloginbtn.setonclicklistener(this);
    }

    public void initview () {
        musertext = (edittext) findviewbyid(r.id.username);
        muserpass = (edittext) findviewbyid(r.id.userpass);
        mloginbtn = (button) findviewbyid(r.id.loginbtn);
    }

    @override
    public void onlogin() {
        mprogressdialog =new progressdialog(this);
        mprogressdialog.show();
    }

    @override
    public void loginsuccess(userbean bean) {
        toast.maketext(this, "登录成功,年龄"+bean.getage(), toast.length_short).show();
    }

    @override
    public void loginfail(exception e, int code) {
        if (mprogressdialog!=null){
            mprogressdialog.dismiss();
            toast.maketext(this, e.getmessage(), toast.length_short).show();
        }
    }

    @override
    public void onclick(view v) {
        switch (v.getid()){
            case r.id.loginbtn:
                if (getuserpass().length()==0 || getusertext().length()==0){
                    toast.maketext(this, "请输入账号或密码", toast.length_short).show();
                    return;
                }
                muserpresenter.userlogin(getusertext(),getuserpass());
                break;
        }
    }
    private string getusertext(){
        return musertext.gettext().tostring();
    }
    private string getuserpass(){
        return  muserpass.gettext().tostring();
    }
}

5.xml布局

<?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"
    android:gravity="center"
    android:background="#ffffff"
    tools:context=".mainactivity">
    <linearlayout
        android:layout_margin="15dp"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <edittext
            android:paddingleft="6dp"
            android:paddingright="6dp"
            android:textcolor="#616161"
            android:id="@+id/username"
            android:inputtype="text"
            android:background="@drawable/shape_editview"
            android:layout_width="match_parent"
            android:layout_height="55dp" />
        <edittext
            android:paddingleft="6dp"
            android:paddingright="6dp"
            android:textcolor="#616161"
            android:layout_margintop="8dp"
            android:id="@+id/userpass"
            android:inputtype="textpassword"
            android:background="@drawable/shape_editview"
            android:layout_width="match_parent"
            android:layout_height="55dp" />
        <button

            android:layout_margintop="8dp"
            android:textcolor="#ffffff"
            android:textsize="19sp"
            android:background="@drawable/shape_login_btn"
            android:text="登录"
            android:id="@+id/loginbtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </linearlayout>
</linearlayout>

image.png

6.两个shape
shape_editview.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="30dp"></corners>
    <solid android:color="#f7f5f5"></solid>
</shape>

shape_login_btn.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <corners android:radius="100dp"></corners>
            <solid android:color="#03a9f4"></solid>
        </shape>
    </item>
    <item android:state_pressed="false">
        <shape>
            <corners android:radius="100dp"></corners>
            <solid android:color="#0a9ddf"></solid>
        </shape>
    </item>
</selector>

以上就是mvp模式得简单实现.一定要多加练习,才能体会掉mvp得优点

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

◎已有 0 人评论

Copyright © 2019  萬仟网 保留所有权利. 粤ICP备17035492号-1
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com