Android 安卓MVP架构(带视频)

引入

M:M层,也就是我们在程序中经常出现的model层,他的功能就是处理数据,其他任务不由他来接手。

V:V层,我们的view层,也就是显示数据的地方,我们在得到数据之后,把数据传递给view层,通过他来显示数据。同时,view层的点击事件等处理会在这里出现,但真正的数据处理不是在这里,而是在model层中处理。

P:P层,也就是Presenter层,他是我们mvp架构中的中间人,通过p层的连接,让我们可以是M层和V层进行通信。M层在获取到数据之后,把它交给P,P层在交给View层,同样,View层的点击事件等处理通过P层去通知M层,让他去进行数据处理。


推荐文章

安卓MVC架构 https://blog.csdn.net/qq_40881680/article/details/101571000


视频讲解:点击下方按钮播放

mvp架构模式讲解(仅限于新手理解)


视频源地址:https://www.bilibili.com/video/av69755230

代码如下
MainActivity:

package com.brk.mvp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View {

    private TextView news;
    private Button add;
    private Button less;

    MainPresenter mainPresenter = new MainPresenter(this);

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

        mainPresenter.show();
        
    }


    @Override
    public void addNum(String string) {
        news.setText(string);
    }

    @Override
    public void lessNum(String string) {
        news.setText(string);
    }

    @Override
    public void showNum(String string) {
        news.setText(string);
    }

    private void initView() {
        news = (TextView) findViewById(R.id.news);
        add = (Button) findViewById(R.id.add);
        less = (Button) findViewById(R.id.less);


        add.setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(android.view.View v) {
                mainPresenter.addNum();
            }
        });

        less.setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(android.view.View v) {
                mainPresenter.lessNum();
            }
        });
    }

}


MainPresenter:

package com.brk.mvp;

public class MainPresenter implements Presenter {

    PeopleBean peopleBean = new PeopleBean("NorthernBrain", 0);

    private View view;

    public MainPresenter(View view) {
        this.view = view;
    }

    @Override
    public void addNum() {

        peopleBean.setNum(peopleBean.getNum() + 1);
        view.addNum(peopleBean.toString());
    }

    @Override
    public void lessNum() {
        peopleBean.setNum(peopleBean.getNum() - 1);
        view.addNum(peopleBean.toString());
    }

    @Override
    public void show() {
        view.showNum(peopleBean.toString());
    }
}


PeopleBean:

package com.brk.mvp;

public class PeopleBean {

    private String name;
    private int num;


    public PeopleBean(String name, int num) {
        this.name = name;
        this.num = num;
    }

    @Override
    public String toString() {
        return "PeopleBean{" +
                "name='" + name + '\'' +
                ", num=" + num +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}


Presenter:

package com.brk.mvp;

public interface Presenter {
    void addNum();

    void lessNum();

    void show();
}


View:

package com.brk.mvp;

public interface View {

    void addNum(String string);

    void lessNum(String string);

    void showNum(String string);
}


布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/news"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="null"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="0dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="8dp"
        android:orientation="horizontal"
        app:layout_constraintTop_toBottomOf="@+id/news"
        tools:ignore="MissingConstraints">

        <Button
            android:id="@+id/add"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="" />

        <Button
            android:id="@+id/less"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="" />

    </LinearLayout>
</android.support.constraint.ConstraintLayout>
发布了101 篇原创文章 · 获赞 1032 · 访问量 76万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 猿与汪的秘密 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览