Android快速开发之appBase——(5).BasePresenter的使用
轉載請注明本文出自JFlex的博客http://blog.csdn.net/jflex/article/details/46456621,請尊重他人的辛勤勞動成果,謝謝!
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)。
| 方法 | 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模塊;再比如需要APICloud云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>() {@Overridepublic void onFailure(HttpException arg0, String arg1) {closeDialog();CommonUtils.showToast(getContext(),R.string.net_error_retry);}@Overridepublic 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 extendsAvFragment<WgouHolder, WgouData, HomeActivity> implementsWgouPresenter.Callback {WgouPresenter wgouService;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);wgouService = new WgouPresenter(fa());wgouService.setHttpReq(fa().getApp().httpReq());wgouService.setCallback(this);}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);wgouService.index();}@Overridepublic void index(IndexResponse response) {_holder.index(response);_holder.scrollBottom();} }這里簡單舉例在Fragment中的使用:
1、首先定義Presenter和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包,直接上源碼。下載地址
總結
以上是生活随笔為你收集整理的Android快速开发之appBase——(5).BasePresenter的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 京东数据采集工具:2022年8月洗衣机品
- 下一篇: Android音乐播放器开发(6)—Li