Android MVP初试
来源:互联网 发布:王权富贵东方月初乐乎 编辑:程序博客网 时间:2024/06/05 08:34
以前开发写代码感觉就是一团糟,基本上一个Activity就能把所有的功能完成,最多就是把一些常用的功能抽取出来组成一个工具类.这个软件的新版本尝试使用MVP模式.第一次尝试使用MVP模式.
以我工作中的一个业务模块为了开发.简单介绍一下该业务功能.业务主要是根据给的ID来获取房屋情况,将信息根据相应的标志展示.
简单的分一下就是:
View层有房屋ID数据输入,展示房屋详细信息,房屋图片信息,经纪人信息,如果访问网络有误有客户提示信息.Activity也作为View处理.
Presenter根据View的需要从Model中获取相应的数据,再将数据调用View展示出来.
Model层基本上是从服务器获取数据,和数据格式化.
PS:图片是网上找的感谢!!!
View层的接口:
/** * View中的数据 * Created by */public interface HouseDetailesView { //获取界面中的楼盘ID int getloupanId(); //界面展示返回的信息 void showMessage(String msg); //界面中展示房子的详细信息 void showHouseInfo(HouseDetailesBean bean); //界面中展示房子图片信息 void showHouseImage(HouseImagesBean layoutImages, ArrayList<String> listFive, int id, int type, int auth); //界面中展示经济人Frgment void showAgentInfo(String phoneNum);}
get开头的方法是presenter从view中获取的一些数据比如,房屋的Id.show开头的方法是Model层获的了数据再通过presenter来对接UI.
View层的Activity实现HouseDetailesView接口,并且持有presenter引用.当刚进入该界面getDetail()方法中会调用presenter中的getHouseDetaileInfo()方法.
/** * 使用mvp模式实现该业务 * 房详情 * Created by */@EActivity(R.layout.activity_house_details)public class HouseDetaileActivity extends BaseActivity implements HouseDetailesView{ @ViewById(R.id.house_label_tv) TextView HouseTitle;//UI的lable //控件初始化 ... @Extra("loupanId") int loupanId;//房源ID private HouseDetailesBean detailesBean; private HouseDetailePresenter detailePresenter; //成员变量 ... @AfterViews void afterView() { detailePresenter = new HouseDetailePresenterImple(this, this); initIcon(); initShareIcon(); getDetail(); } private void initIcon() { ... } /** * 初始化分享 */ private void initShareIcon() { ... } /** * 根据要展示的二手房源的ID来查询该房的详细信息 */ @Background(id = "secondHouse_detail") public void getDetail() { showLoadingProgress("secondHouse_detail"); //获取详细信息 detailePresenter.getHouseDetaileInfo(); closeLoadingProgress(); } @Override public int getloupanId() { return loupanId; } @UiThread @Override public void showMessage(String msg) { showToast(msg); } **//UI中数据填充是应该放在这里,还是应该放在Presenter中????????????** @UiThread @Override public void showHouseInfo(HouseDetailesBean bean) { detailesBean = bean; housePrice.setText(bean.getRmb_price()); space.setText(bean.getArea()); //其他数据填充 ... getHouseImage(); } @Background void getHouseImage() { detailePresenter.getImage(); } @UiThread @Override public void showHouseImage(HouseImagesBean layoutImages, ArrayList<String> listFive, int id, int type, int auth) { showHaofangtuoHouseAlbum(layoutImages, listFive, id, type, auth); } /** * 底部经纪人信息 */ @UiThread @Override public void showAgentInfo(String phoneNum) { ... } //其他方法 ...}
Presenter层的接口:
/** * Created by */public interface HouseDetailePresenter { /** * 展示错误信息 * @param mag */ void showInfoError(String mag); /** * 展示房子详细信息 * @param entity */ void showInfo(HouseDetailesEntity entity); /** * 展示房子图片信息 */ void showImage(HaofangtuoImageListEntity entity); /** * 展示经纪人信息 */ void showAgentInfo(String phoneNum); /** * 获取海外房信息 * @return */ void getHouseDetaileInfo(); /** * 获取图片信息 * @return */ void getImage(); void shareInfo(HouseDetailesBean detailesBean, ShareViewIcons shareIcons);}
HouseDetailePresenterImple实现HouseDetailePresenter接口,
HouseDetailePresenterImple持有M层HouseDetailsModel,和
View层HouseDetailesView的引用.
刚进入界面Activity的getDetail()方法中调用presenter中HouseDetailePresenterImple的getHouseDetaileInfo()方法.
/** * 获取房详情信息 */ @Override public void getHouseDetaileInfo() { detailsModel.getHouseDetail(detailesView.getloupanId()); }/** * 展示房子详细信息 * @param entity */@Overridepublic void showInfo(HouseDetailesEntity entity) { if (null != entity && entity.isOk() && null != entity.getData()) { bean = entity.getData(); detailesView.showHouseInfo(bean); detailesView.showAgentInfo(bean.getHotline()); } else { detailesView.showMessage(entity.getMsg()); }}
getHouseDetaileInfo()方法中会调用M层的getHouseDetail(int loupanId),获取数据逻辑就在该方法中.
Model层接口
/** * 房详情中使用到的接口 * Created by */public interface HouseDetailsModel { void getHouseDetail(int loupanId);//获取房详情接口 void getImgList(int loupanId);//获取房详情中spanner中原始图片接口}
HouseDetailesModelImple实现HouseDetailsModel接口,持有HouseDetailePresenter引用,类似回调,当数据获取成功后需要改变UI,调用HouseDetailePresenter中相应的方法.
HouseDetailesModelImple.java中的getHouseDetail()方法逻辑如下,从网上获取json数据后使用fastJson解析获得实体数据,再根据数据返回标志是否展示其他相应信息.当数据获取正确时就需要通过presenter中的show方法来展示相应的信息.
HouseDetailesModelImple.java中getHouseDetail方法如下
@Overridepublic void getHouseDetail(int loupanId) { HouseDetailesEntity entity = Api().getHouseDetail(loupanId); detailePresenter.showInfo(entity); }
以上就基本把大致的逻辑解释完毕.
MVP与MVC,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model数据管理,View视图。
MVP中View不直接与Model层交互,是两者的桥梁,所有的交互都发生在Presenter内部,而在MVC中View可以直接从Model中读取数据.
MVP的好处就低耦合,健壮等等,不好的地方其一是文件多了不少.
第一次使用MVP有些逻辑分离的并不是很完美,这份代码分离修改了好几次才是现在这样的结果,还需要在以后的开发中不断的加强.
PS:看了好多MVP的DEMO都是以登录作为实例,是不是这个功能是经典案例?
我是菜鸟,希望大家多多指教,互相交流,共同进步!
- Android MVP初试
- 初试Android的MVP框架
- 初试Android
- Android初试
- android 抽屉功能初试
- Android初试Volley
- Android UiAutomator 初试。
- Android Animation初试
- Android Realm初试
- Android AIDL 初试
- android MVP
- android mvp
- android MVP
- Android MVP
- Android MVP
- android MVP
- Android MVP
- Android MVP
- log4j基础详解及示例
- mapreduce(七):hadoop二次排序
- Drop Table恢复:Oracle中误删表后还原表
- 【转】oracle中exp,imp的使用详解
- 权威大数据应用解决方案
- Android MVP初试
- Failed to load class "org.slf4j.impl.StaticLoggerBinder"异常解决办法
- 3.While observing the index statistics, you find that an index is highly fragmented, thereby resulti
- Linux安装MySQL的两种方法
- Log4j讲解
- linux添加路由
- 字符串格式化显示
- xml格式数据解析
- 社会化分享在QQ互联后台的URL schema应该怎么设置?