Android快速開發之appBase——(5).BasePresenter的使用,androidbase64
Android快速開發之appBase——(5).BasePresenter的使用
Presenter是來自MVP中的概念,是用來處理與使用者互動的邏輯。在這裡更加簡單化,Presenter中的方法是根據業務來定義,比如擷取訊息列表,那麼業務常常會這樣:先去請求網路,網路正常請求到資料返回並展示在UI層,網路錯誤沒有拿到資料,看看緩衝中有沒有,然後從緩衝中拿到資料並返回並展示在UI層;突然,有一天業務需求發生變化,只允許擷取網路,網路錯誤UI上顯示沒有訊息。如果之前在UI層已經做過資料為空白的處理,那麼UI層就不用修改任何代碼,僅僅只需要修改presenter層,這樣就將UI層和業務層區分,並且耦合降低了。
1、概述
BasePresenter僅僅是提取的一個概念,實現的方式有很多種,在這裡我採用callback機制,presenter和callback中的方法是對應存在的,比如presenter中getProductsByType(int type),那麼這個方法主題中通過非同步處理資料,處理完成之後將資料通過callback回傳給setProductsByType(Object result)。
| 類或介面 |
presenter |
callback |
| 方法 |
getProductsByType(int type) |
setProductsByType(Object result) |
| 執行所線上程 |
非UI線程 |
UI線程 |
2、代碼
package com.snicesoft.presenter;import android.content.Context;import com.snicesoft.util.NetworkUtil;public class BasePresenter<C extends BasePresenter.Callback> { public interface Callback { } private Context context; protected C callback; public void setCallback(C callback) { this.callback = callback; } public BasePresenter(Context context) { this.context = context; } public boolean isNetConnect() { return NetworkUtil.isConnect(getContext()); } public Context getContext() { return context; }}
- 代碼採用內部介面定義,為了減少代碼整體風格不那麼臃腫。當然,也可以按照自己的編碼風格自訂。
- 欄位說明:context只是為了方便操作一些常用的業務,比如上面提到的網路連接判斷。欄位都可以按照自己的需求添加,比如這個presenter中需要網路請求,那麼可以添加HttpReq模組;再比如需要APICloudCloud API請求,可以添加APICloudSDK模組。
3、使用範圍
常用範圍
- activity:實現callback介面,定義callback所在presenter的對象欄位,在onCreate中初始化。
- fragment:實現callback介面,定義callback所在presenter的對象欄位,在onCreate中初始化。
原則上,哪裡需要就寫哪裡。
4、樣本
WgouPresenter.java
package com.haier.rrmaker.ui.home.fragment.presenter;import android.app.ProgressDialog;import android.content.Context;import com.alibaba.fastjson.JSON;import com.haier.rrmaker.R;import com.haier.rrmaker.http.HttpParams;import com.haier.rrmaker.http.HttpResult;import com.haier.rrmaker.http.response.IndexResponse;import com.lidroid.xutils.exception.HttpException;import com.lidroid.xutils.http.ResponseInfo;import com.lidroid.xutils.http.callback.RequestCallBack;import com.snicesoft.http.HttpReq;import com.snicesoft.presenter.BasePresenter;import com.snicesoft.util.CommonUtils;import com.snicesoft.util.DialogUtil;public class WgouPresenter extends BasePresenter<WgouPresenter.Callback> { public interface Callback extends BasePresenter.Callback { void index(IndexResponse response); } HttpReq httpReq; public void setHttpReq(HttpReq httpReq) { this.httpReq = httpReq; } ProgressDialog progressDialog; public WgouPresenter(Context context) { super(context); progressDialog = DialogUtil.getProgressDialog(context); } protected void showDialog(CharSequence message, boolean... flag) { if (flag != null) { if (flag.length > 0) progressDialog.setCancelable(flag[0]); if (flag.length > 1) progressDialog.setCanceledOnTouchOutside(flag[1]); } progressDialog.setMessage(message); progressDialog.show(); } protected void closeDialog() { if (progressDialog.isShowing()) progressDialog.dismiss(); } public void index() { if (httpReq == null) return; if (isNetConnect()) { showDialog("正在載入"); httpReq.POST(HttpParams.Wgou.Index, null, new RequestCallBack<String>() { @Override public void onFailure(HttpException arg0, String arg1) { closeDialog(); CommonUtils.showToast(getContext(), R.string.net_error_retry); } @Override public void onSuccess(ResponseInfo<String> arg0) { closeDialog(); IndexResponse response = JSON.parseObject( arg0.result, IndexResponse.class); if (HttpResult.isSuccess(response)) { callback.index(response); } else { CommonUtils.showToast(getContext(), "資料返回錯誤"); } } }); } else { CommonUtils.showToast(getContext(), R.string.net_error); } }}
WgouFragment.java
public class WgouFragment extends AvFragment<WgouHolder, WgouData, HomeActivity> implements WgouPresenter.Callback { WgouPresenter wgouService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); wgouService = new WgouPresenter(fa()); wgouService.setHttpReq(fa().getApp().httpReq()); wgouService.setCallback(this); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); wgouService.index(); } @Override public void index(IndexResponse response) { _holder.index(response); _holder.scrollBottom(); }}
這裡簡單舉例在Fragment中的使用:
1、首先定義Presenter和Callback
| 類或介面 |
WgouPresenter |
WgouPresenter.Callback |
說明 |
| 方法 |
index() |
index(IndexResponse response) |
擷取首頁資訊 |
| 執行所線上程 |
非UI線程 |
UI線程 |
|
2、WgouFragment實現WgouPresenter.Callback
實現index(IndexResponse response)方法,將返回的資料再此方法綁定到對應的UI上。如果業務在開發之前充分溝通,這塊完全可以類比資料進行測試,後期線上上測試環境調試。
對於WgouPresenter的定義在onCreate初始化。onActivityCreated方法中進行index()請求,這隻是做個示範。但是請求順序一定不能錯誤:必須在WgouPresenter初始化完畢並且View初始化完畢(也就是Holder初始化完畢)
5、最後
一定要注意規範,否則會導致代碼混亂。對於這套規範我寫了個簡單的代碼產生器,產生activity和fragment的時候會將holder、data、presenter全部產生好,省去了自己建立的麻煩。由於編譯環境不同,故不提供jar包,直接上源碼。