Android快速开发之appBase——(5).BasePresenter的使用
轉(zhuǎn)載請(qǐng)注明本文出自JFlex的博客http://blog.csdn.net/jflex/article/details/46456621,請(qǐng)尊重他人的辛勤勞動(dòng)成果,謝謝!
Android快速開發(fā)之a(chǎn)ppBase——(5).BasePresenter的使用
Presenter是來(lái)自MVP中的概念,是用來(lái)處理與用戶交互的邏輯。在這里更加簡(jiǎn)單化,Presenter中的方法是根據(jù)業(yè)務(wù)來(lái)定義,比如獲取消息列表,那么業(yè)務(wù)常常會(huì)這樣:先去請(qǐng)求網(wǎng)絡(luò),網(wǎng)絡(luò)正常請(qǐng)求到數(shù)據(jù)返回并展示在UI層,網(wǎng)絡(luò)錯(cuò)誤沒(méi)有拿到數(shù)據(jù),看看緩存中有沒(méi)有,然后從緩存中拿到數(shù)據(jù)并返回并展示在UI層;突然,有一天業(yè)務(wù)需求發(fā)生變化,只允許獲取網(wǎng)絡(luò),網(wǎng)絡(luò)錯(cuò)誤UI上顯示沒(méi)有消息。如果之前在UI層已經(jīng)做過(guò)數(shù)據(jù)為空的處理,那么UI層就不用修改任何代碼,僅僅只需要修改presenter層,這樣就將UI層和業(yè)務(wù)層區(qū)分,并且耦合降低了。1、概述
BasePresenter僅僅是提取的一個(gè)概念,實(shí)現(xiàn)的方式有很多種,在這里我采用callback機(jī)制,presenter和callback中的方法是對(duì)應(yīng)存在的,比如presenter中g(shù)etProductsByType(int type),那么這個(gè)方法主題中通過(guò)異步處理數(shù)據(jù),處理完成之后將數(shù)據(jù)通過(guò)callback回傳給setProductsByType(Object result)。
| 方法 | getProductsByType(int type) | setProductsByType(Object result) |
| 執(zhí)行所在線程 | 非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;} }- 代碼采用內(nèi)部接口定義,為了減少代碼整體風(fēng)格不那么臃腫。當(dāng)然,也可以按照自己的編碼風(fēng)格自定義。
- 字段說(shuō)明:context只是為了方便操作一些常用的業(yè)務(wù),比如上面提到的網(wǎng)絡(luò)連接判斷。字段都可以按照自己的需求添加,比如這個(gè)presenter中需要網(wǎng)絡(luò)請(qǐng)求,那么可以添加HttpReq模塊;再比如需要APICloud云API請(qǐng)求,可以添加APICloudSDK模塊。
3、使用范圍
常用范圍- activity:實(shí)現(xiàn)callback接口,定義callback所在presenter的對(duì)象字段,在onCreate中初始化。
- fragment:實(shí)現(xiàn)callback接口,定義callback所在presenter的對(duì)象字段,在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(), "數(shù)據(jù)返回錯(cuò)誤");}}});} 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();} }這里簡(jiǎn)單舉例在Fragment中的使用:
1、首先定義Presenter和Callback
| 方法 | index() | index(IndexResponse response) | 獲取首頁(yè)信息 |
| 執(zhí)行所在線程 | 非UI線程 | UI線程 |
2、WgouFragment實(shí)現(xiàn)WgouPresenter.Callback
實(shí)現(xiàn)index(IndexResponse response)方法,將返回的數(shù)據(jù)再此方法綁定到對(duì)應(yīng)的UI上。如果業(yè)務(wù)在開發(fā)之前充分溝通,這塊完全可以模擬數(shù)據(jù)進(jìn)行測(cè)試,后期在線上測(cè)試環(huán)境調(diào)試。
對(duì)于WgouPresenter的定義在onCreate初始化。onActivityCreated方法中進(jìn)行index()請(qǐng)求,這只是做個(gè)演示。但是請(qǐng)求順序一定不能錯(cuò)誤:必須在WgouPresenter初始化完畢并且View初始化完畢(也就是Holder初始化完畢)
5、最后
一定要注意規(guī)范,否則會(huì)導(dǎo)致代碼混亂。對(duì)于這套規(guī)范我寫了個(gè)簡(jiǎn)單的代碼生成器,生成activity和fragment的時(shí)候會(huì)將holder、data、presenter全部生成好,省去了自己創(chuàng)建的麻煩。 由于編譯環(huán)境不同,故不提供jar包,直接上源碼。下載地址
總結(jié)
以上是生活随笔為你收集整理的Android快速开发之appBase——(5).BasePresenter的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 京东数据采集工具:2022年8月洗衣机品
- 下一篇: Android音乐播放器开发(6)—Li