采用MVP構(gòu)架县踢,讓activity只做ui的變化,數(shù)據(jù)的展示修然,讓presenter做數(shù)據(jù)的處理,數(shù)據(jù)的傳遞质况,讓Model做數(shù)據(jù)的網(wǎng)絡(luò)獲取愕宋。
這樣就可以將原本全部存在Activity的成百上千行代碼分離出來,增加代碼可讀性结榄。只不過增加了類的個(gè)數(shù)中贝。
下面演示一下流程圖
展示一下實(shí)現(xiàn)Activity界面和數(shù)據(jù)拆分的幾個(gè)類
MvpCallBack接口
MvpCallback接口是Model向presenter層傳遞數(shù)據(jù)的載體:
public interface MvpCallback {
/**
* 數(shù)據(jù)請(qǐng)求成功
* @param data
*/
void onSuccess(String data);
/**
* 數(shù)據(jù)請(qǐng)求成功 解析失敗
* @param msg
*/
void onFailure(String msg);
/**
* 數(shù)據(jù)請(qǐng)求失敗
*/
void onError();
/**
* 一次請(qǐng)求完成 不管成功或者失敗
*/
void onComplete();
}
MvpModel中提供了向網(wǎng)絡(luò)獲取數(shù)據(jù)的方法,這里用handler.postDelay實(shí)現(xiàn)一個(gè)延時(shí)操作來模擬網(wǎng)絡(luò)請(qǐng)求的情況
public class MvpModel {
public enum Action{
NORMAL,FAILUER,ERROR
}
public static void getNetData(final Action param,final MvpCallback callback){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
switch (param){
case NORMAL:
callback.onSuccess("請(qǐng)求成功");
break;
case FAILUER:
callback.onFailure("請(qǐng)求失敗臼朗,參數(shù)有誤");
break;
case ERROR:
callback.onError();
break;
}
callback.onComplete();
}
},2000);
}
MvpView接口是Presenter層向Activity層傳遞數(shù)據(jù)的載體邻寿,在presenter接受網(wǎng)絡(luò)數(shù)據(jù)響應(yīng)之后通過View接口讓activity更新界面。
public interface MvpView {
/**
* 顯示正在加載進(jìn)度框
*/
void showLoading();
/**
* 隱藏正在加載進(jìn)入框
*/
void hideLoading();
/**
* 請(qǐng)求數(shù)據(jù)成功后视哑,調(diào)用此接口顯示數(shù)據(jù)
* @param data
*/
void showData(String data);
/**
* 數(shù)據(jù)請(qǐng)求失敗绣否,調(diào)用
* @param msg
*/
void showFailureMessage(String msg);
/**
* 當(dāng)數(shù)據(jù)請(qǐng)求異常,調(diào)用此接口提示
*/
void showErrorMessage();
}
Presenter類是具體的邏輯業(yè)務(wù)處理類,Presenter類的構(gòu)造方法中有一個(gè)View接口的參數(shù)挡毅,是為了能夠通過View接口通知Activity進(jìn)行更新界面等操作蒜撮。
public class MvpPresenter {
//View接口
private MvpView mvpView;
public MvpPresenter(MvpView view){
this.mvpView=view;
}
public void getData(MvpModel.Action param){
mvpView.showLoading();
MvpModel.getNetData(param, new MvpCallback() {
@Override
public void onSuccess(String data) {
mvpView.showData(data);
}
@Override
public void onFailure(String msg) {
mvpView.showFailureMessage(msg);
}
@Override
public void onError() {
mvpView.showErrorMessage();
}
@Override
public void onComplete() {
mvpView.hideLoading();
}
});
}
}
MainActivity 繼承了一個(gè)MvpView類 并且定義了一個(gè)MvpPresenter對(duì)象
public class MainActivity extends AppCompatActivity implements MvpView {
private TextView tv_s;
private TextView tv_f;
private TextView tv_e;
ProgressDialog progressDialog;
MvpPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("yzh","1111111");
tv_s=(TextView)findViewById(R.id.tv_s);
tv_s.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
presenter.getData(MvpModel.Action.NORMAL);
}
});
tv_f=(TextView)findViewById(R.id.tv_f);
tv_f.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
presenter.getData(MvpModel.Action.FAILUER);
}
});
tv_e=(TextView)findViewById(R.id.tv_e);
tv_e.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
presenter.getData(MvpModel.Action.ERROR);
}
});
// 初始化進(jìn)度條
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
progressDialog.setMessage("正在加載數(shù)據(jù)");
//初始化Presenter
presenter = new MvpPresenter(this);
//presenter2綁定view
// presenter2=new MvpPresenter2();
// presenter2.attachView(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
// presenter2.detachView();
}
@Override
public void showLoading() {
if(!progressDialog.isShowing()){
progressDialog.show();
}
}
@Override
public void hideLoading() {
if(progressDialog.isShowing()){
progressDialog.dismiss();
}
}
@Override
public void showData(String data) {
if(!TextUtils.isEmpty(data)){
Toast.makeText(MainActivity.this,data,Toast.LENGTH_SHORT).show();
}
}
@Override
public void showFailureMessage(String msg) {
if(!TextUtils.isEmpty(msg)){
Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
}
}
@Override
public void showErrorMessage() {
Toast.makeText(MainActivity.this,"網(wǎng)絡(luò)請(qǐng)求出錯(cuò)",Toast.LENGTH_SHORT).show();
}
}
下一步將提取冗余代碼做父類,并且使用泛型數(shù)據(jù)跪呈,來防止多樣的數(shù)據(jù)類型導(dǎo)致更多類的生產(chǎn)段磨。
MVP框架搭建(2):mvp的分塊演示