Android MVC,MVP思想及实例

Posted 伟雪无痕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android MVC,MVP思想及实例相关的知识,希望对你有一定的参考价值。

一.MVC简介

1.MVC概念

MVC (Model-View-Controller, 模型-视图-控制器)

Model: 数据层, 负责处理业务逻辑,例如监听网络与数据库接口.
View: 界面(UI)层, 显示数据,在android中一般指xml
Contoller: 逻辑层, 传递用户的交互和更新数据,在Android中通常指Activity和Fragment

2.MVC请求流程

1).View接受User的request,eg : View:OnClick
2).View传递request给Controller,eg: UserControll:loadUserData
3).Controller操作Model进行数据更新eg :UserModel.LoadUserDataModel
4).Model通知View变化,eg:MvcMainActivity:updateTextView
5).View获取数据进行相应显示

3.demo实例

1).mvc_activity_main.xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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="Mvc Test"
        android:textSize="30sp"
        android:layout_gravity="center_horizontal"
        />

    <Button
        android:id="@+id/addUser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="add User"
        ></Button>


    <TextView
        android:id="@+id/mId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="user id"
        android:visibility="gone"/>

    <TextView
        android:id="@+id/mName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="user name"
        android:visibility="gone"/>

</LinearLayout>

2).UserControll.java

package design.mvc;

import design.MvcMainActivity;
import design.UserBean;

public class UserControll 
    private MvcMainActivity mMainActivity;
    private UserModel mUserModel;

    public UserControll(MvcMainActivity mainActivity)
        this.mMainActivity=mainActivity;
    

    public void loadUserData(UserBean userBean)
        mUserModel=new UserModel(mMainActivity);
        mUserModel.LoadUserDataModel(userBean);
    

3).UserModel.java

package design.mvc;


import design.MvcMainActivity;
import design.UserBean;

public class UserModel 
    public UserBean mUserBean;
    MvcMainActivity mMainActivity;

    public UserModel(MvcMainActivity mainActivity)
        this.mMainActivity=mainActivity;
    

    public void LoadUserDataModel(UserBean userBean)
        mUserBean=userBean;
        mMainActivity.updateTextView(this);
    

4).UserBean.java

package design;

import java.io.Serializable;

public class UserBean implements Serializable 

    private String id;
    private String name;

    public UserBean(String id, String name) 
        this.id = id;
        this.name = name;
    

    public String getId() 
        return id;
    

    public void setId(String id) 
        this.id = id;
    

    public String getName() 
        return name;
    

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

    @Override
    public String toString() 
        return "UserBean" +
                "id='" + id + '\\'' +
                ", name='" + name + '\\'' +
                '';
    

5).MvcMainActivity.java

package design;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import design.mvc.UserControll;
import design.mvc.UserModel;
import com.example.R;

public class MvcMainActivity extends AppCompatActivity implements View.OnClickListener 

    private TextView mIdText;
    private TextView mNameText;
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mvc_activity_main);
        initViews();
    

    private void initViews() 
        mButton=findViewById(R.id.addUser);
        mButton.setOnClickListener(this);
        mIdText=findViewById(R.id.mId);
        mNameText=findViewById(R.id.mName);
    

    @Override
    protected void onPause() 
        mIdText.setVisibility(View.GONE);
        mNameText.setVisibility(View.GONE);
        super.onPause();
    

    public void updateTextView(UserModel userModel)
        mIdText.setText(userModel.mUserBean.getId());
        mNameText.setText(userModel.mUserBean.getName());
    

    @Override
    public void onClick(View view) 

        switch (view.getId())
            case R.id.addUser:
                UserControll mControll=new UserControll(this);
                UserBean userBean=new UserBean("0001","jessie");
                mControll.loadUserData(userBean);
                mIdText.setVisibility(View.VISIBLE);
                mNameText.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        
    

 

二.MVP简介

1.MVP概念

 MVP (Model-View-Presenter)

Model: 数据层, 专注于数据逻辑
View: UI模块,负责界面显示和与用户交汇,在Android中,可能是指Activity、Fragment或者View
Presenter: 负责业务逻辑,起着连接View和Model桥梁的作用

1).View接受User的request,eg: View:OnClick  
2).View调用Presenter的click,eg: PresenterImpl:onButtonClick
3).Model进行数据更新并传递给Presenter,eg: ModelImpl.getNextShow
4).Presenter接收Model的数据后,通知view更新,eg:PresenterImpl:onFinished
5).View获取数据进行相应显示eg:MvpMainActivity.setViewStr/MvpMainActivity.hidProgress

2.demo实例

1).activity_mvp_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:layout_margin="5dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mvp Test"
        android:textSize="30sp"
        />

    <TextView
        android:id="@+id/description"
        android:layout_below="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="descrip" />
    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:layout_centerInParent="true"
        ></ProgressBar>

    <Button
        android:id="@+id/click_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Click show"
        android:gravity="center_horizontal"
        ></Button>


</RelativeLayout>

2).Model.java

package design.mvp;

public interface Model 
    interface OnFinishedListener
        void onFinished(String string);
    

    void getNextShow(OnFinishedListener onFinishedListener);

3).View.java 

package design.mvp;

public interface View 
    void showProgress();
    void hidProgress();
    void setViewStr(String string);

4).Presenter.java

package design.mvp;

public interface Presenter 
    void onButtonClick();
    void onDestroy();

5).ModelImpl.java 

package design.mvp;

import android.os.Handler;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class ModelImpl implements Model 

    private List<String> arrayList = Arrays.asList(
            "Children love Write the Room games because they love to wander around the room, move, and get active",
            "Here are three Write the Room activities that Pre-K children can do. " +
                    "From the second the kids began this activity, excitement filled the air!",
            "Rainbow Write the Room was a huge hit." +
                    "Kids who never attempted to write or even draw, were suddenly excited about it!",
            "Attach these all around the room to the walls and on furniture. " +
                    "Loops of masking tape on the back worked great,",
            "two pieces on the back were enough. " +
                    "Give each child in the group a different color crayon, and ask them to only use that color."
    );

    @Override
    public void getNextShow(OnFinishedListener onFinishedListener) 
        new Handler().postDelayed(new Runnable() 
            @Override
            public void run() 
                onFinishedListener.onFinished(getRandomStr());
            
        ,2000);

    

    private String getRandomStr()
        Random random=new Random();
        int index=random.nextInt(arrayList.size());
        return arrayList.get(index);
    

6).PresenterImpl.java 

package design.mvp;

public class PresenterImpl implements Presenter,Model.OnFinishedListener 

    private View mView;
    private Model mModel;

    public PresenterImpl(View view, Model model) 
        this.mView = view;
        this.mModel = model;
    

    @Override
    public void onFinished(String str) 
        if (mView!=null)
            mView.setViewStr(str);
            mView.hidProgress();
        
    

    @Override
    public void onButtonClick() 
        if (mView!=null)
            mView.showProgress();
        
        mModel.getNextShow(this);

    

    @Override
    public void onDestroy() 
        mView=null;
    

7).MvpMainActivity.java

package design;

import android.os.Bundle;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.R;

import design.mvp.ModelImpl;
import design.mvp.PresenterImpl;
import design.mvp.View;

public class MvpMainActivity extends AppCompatActivity implements View, android.view.View.OnClickListener 

    private TextView mTextView;
    private Button mButton;
    private ProgressBar mProgressBar;

    private PresenterImpl mPresenterImpl;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mvp_main);
        initViews();
    

    private void initViews() 
        mTextView=findViewById(R.id.description);
        mButton=findViewById(R.id.click_show);
        mButton.setOnClickListener(this);
        mProgressBar=findViewById(R.id.progress);
        mPresenterImpl=new PresenterImpl(this,new ModelImpl());
    

    @Override
    protected void onDestroy() 
        if (mPresenterImpl!=null)
            mPresenterImpl.onDestroy();
        
        super.onDestroy();
    

    @Override
    public void showProgress() 
        mProgressBar.setVisibility(android.view.View.VISIBLE);
        mTextView.setText(android.view.View.GONE);
    

    @Override
    public void hidProgress() 
        mProgressBar.setVisibility(android.view.View.GONE);
        mTextView.setText(android.view.View.VISIBLE);
    

    @Override
    public void setViewStr(String str) 
        mTextView.setText(str);
    

    @Override
    public void onClick(android.view.View view) 
        switch (view.getId())
            case R.id.click_show:
                mPresenterImpl.onButtonClick();
                break;
            default:
                break;
        
    

以上是关于Android MVC,MVP思想及实例的主要内容,如果未能解决你的问题,请参考以下文章

Android MVP-编程思想4(AOP思想-动态代理运用,反射创建M层实例对象)

结合实例分析Android MVP的实现

Android MVP-编程思想1(什么是MVC-MVP-MVVM模式?)

Android 架构MVC MVP MVVM+实例

android MVP——mvp架构的应用和优化

Android进阶之路-详解MVP