Android快速开发之appBase

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;<.{}private Context context;protected C callback;(C callback) {this.callback = callback;}public BasePresenter(Context context) {this.context = context;}() {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;<.Callback {void index(IndexResponse response);}HttpReq httpReq;(HttpReq httpReq) {this.httpReq = httpReq;}ProgressDialog progressDialog;public WgouPresenter(Context context) {super(context);progressDialog = DialogUtil.getProgressDialog(context);}(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();}() {if (progressDialog.isShowing())progressDialog.dismiss();}() {if (httpReq == null)return;if (isNetConnect()) {showDialog(“正在加载”);httpReq.POST(HttpParams.Wgou.Index, null,new RequestCallBack<String>() {(HttpException arg0, String arg1) {closeDialog();CommonUtils.showToast(getContext(),R.string.net_error_retry);}(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

<WgouHolder, WgouData, HomeActivity> implementsWgouPresenter.Callback {WgouPresenter wgouService;(Bundle savedInstanceState) {super.onCreate(savedInstanceState);wgouService = new WgouPresenter(fa());wgouService.setHttpReq(fa().getApp().httpReq());wgouService.setCallback(this);}(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);wgouService.index();}(IndexResponse response) {_holder.index(response);_holder.scrollBottom();}}

这里简单举例在Fragment中的使用: 1、首先定义Presenter和Callback

类或接口 WgouPresenter WgouPresenter.Callback 说明

方法 index() index(IndexResponse response) 获取首页信息

执行所在线程 非UI线程 UI线程

人之所以有一张嘴,而有两只耳朵,原因是听的要比说的多一倍。

Android快速开发之appBase

相关文章:

你感兴趣的文章:

标签云: