安卓 MVP框架

什么是MVP

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

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

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

public interface ReceiveAddressListView {

    public  void  setRectViewData(List<AddressList>  viewData);
}

代码中存在recyclerview,需要给列表设置数据源,抽象一个方法出来,如果要给webview 加载URL,我们可以抽象一个方法出来

定义了m,v,p三个接口后,然后分别来实现
m:

package com.test.mvp.mode;

/**
 * Created by Administrator on 2017/8/7.
 */

public class AddressList {
    public String  name;
    public String  phone;
    public String  address;

    public AddressList(String name, String phone, String address) {
        this.name = name;
        this.phone = phone;
        this.address = address;
    }

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

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    public String getAddress() {
        return address;
    }
}
 

让p层持有view ,在activity中只会看到调用p层方法,完成逻辑处理

package com.test.mvp.presenter;

import android.os.Handler;

import com.test.mvp.GetDataUtil;
import com.test.mvp.ReceiveAddressListActivity;
import com.test.mvp.mode.AddressList;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/8/7.
 */

public class ReceiveAddressListPresenter implements BasePresenter{
    private Handler  handler=new Handler();
    //下拉刷新状态
    public final  int REFRESH_STATUS=1;
    public boolean  isLoad=false;
    //上拉加载更多状态
    public final  int LOADING_STATUS=2;
    private final ReceiveAddressListActivity receiveAddressListActivity;
    private  ArrayList<AddressList>  addressLists=new ArrayList<>();
    public ReceiveAddressListPresenter(ReceiveAddressListActivity receiveAddressListActivity) {
        this.receiveAddressListActivity=receiveAddressListActivity;
    }

    @Override
    public void getPageData(final int state, final int page) {//这个地方进行网络请求

        if (!isLoad)
        {
            isLoad=true;
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ArrayList<AddressList>  lists=  GetDataUtil.getAddressList(page);
                    if (state==REFRESH_STATUS)//刷新数据
                    {
                        addressLists=lists;
                    }else {//上拉加载
                        addressLists.addAll(lists);
                    }
                    receiveAddressListActivity.setRectViewData(addressLists);
                    isLoad=false;
                }
            },1000);
        }

    }

}
 ```java
mvp模式的实现,先是用接口方式声明自己要什么,然后实现类来具体干什么。 
按照这个思路,我们基本可以确定v,p层的抽象方法,又可以说是接口层。m层去干什么呢,在实际开发中 
并不需要去写一个接口model然后来实现,所以一般没写

准备好了接口方法,如何实现mvp,我们需要让activity实现ReceiveAddressListView,并且重载setRectViewData(); 这里实现如何去设置列表数据源。

同样ReceiveAddressListPresenter实现BasePresenter 接口完成p层实现,重载 
getPageData();,p层持有v层 

public ReceiveAddressListPresenter(ReceiveAddressListActivity receiveAddressListActivity) {
this.receiveAddressListActivity=receiveAddressListActivity;
}
1
2
3
public ReceiveAddressListPresenter(ReceiveAddressListView receiveAddressListActivity) {
this.receiveAddressListActivity=receiveAddressListActivity;
}

你可能感兴趣的:(安卓 MVP框架)