少年意气强不羁,虎胁插翼白日飞。这篇文章主要讲述Android MVP 架构一 View与Presenter相关的知识,希望能为你提供帮助。
View:主要负责界面的显示及跟数据无关的逻辑,比如设置控件的点击事件等
Presenter:主要负责View与Model的交互
Model:数据部分
-------
MVP的核心是:
View层不持有Model层对象任何引用,当然参数里面和临时变量里可以有Model层对象,只持有Presenter层对象引用,任何需要更新或者操作数据的,都间接通过Presenter对象去操作数据。而Model层想要操作View层是无法实现的,必须通过Presenter层。
Presenter层持有View层对象的引用,除此之外不持有其他的UI控件等的引用,Model层会把想要更新View的操作委托Presenter去操作,而Presenter层会把更新View操作交给View层对象去操作。
-------
1、view 层,实现:
public class GoodsActivity extends Activity implements ViewPager.OnPageChangeListener, OnPageClickListener, ShoppingMarkContentView {
2、ShoppingMarkContentView :
package com.wbh.mall.views.iviews; import com.wbh.mall.restful.bean.GoodDTO; import com.wbh.mall.restful.bean.UserDataListDTO; import java.util.List; public abstract interface ShoppingMarkContentView { public abstract void refershRecommendItemList(List< GoodDTO> paramList); public abstract void refreshCollectSameMarkUserInfo(UserDataListDTO paramUserDataListDTO); public abstract void refreshItemDetail(GoodDTO paramGoodDTO); public abstract void setFollow(short paramShort); }
3、在GoodsActivity 中实现上面的方法:
@Override @SuppressLint({"MissingSuperCall"}) protected void onResume() { super.onResume(); this.markContentPresenter.start(); }@Override public void refershRecommendItemList(List< GoodDTO> paramList) { runOnUiThread(new Runnable() { @Override public void run() { if ((paramList != null) & & (!paramList.isEmpty())) { GoodsActivity.this.recommend_good_container.setVisibility(0); GoodsActivity.this.recommendGoodListAdapter.goodDTOS.addAll(paramList); GoodsActivity.this.recommend_good_list_recyclerview.notifyFinish(); } } }); }
文章图片
文章图片
文章图片
其实在他上面有个入口:
@Override public void onCreate(Bundle savedInstanceState) { this.markContentPresenter.tuijianGoods(false);
public void tuijianGoods(boolean b) {if (!b) { this.manager.current_page = 1; this.lastUpDate = DateUtil.getCurrentTime(); }int j = this.manager.page_num; int i = this.manager.current_page; long l = this.lastUpDate; String str = this.articleId; if (TextUtils.isEmpty(this.itemUrl) == true) {} StringBuilder sb = new StringBuilder(); sb.append(this.num_iid); sb.append(""); markSource.tuiJianGoods(j, i, l, str, sb.toString()); }
在 public class ShoppingMarkContentPresenter extends Presenter {中定义上面的方法;
4、请求服务器:
public class MarkRestSource extends RestSource {
public void tuiJianGoods(int paramInt1, int paramInt2, long paramLong, String paramString1, String paramString2) { Map localMap = createPhoneInfoMap(); localMap.put("pageSize", Integer.valueOf(paramInt1)); localMap.put("pageNo", Integer.valueOf(paramInt2)); localMap.put("lastUpDate", Long.valueOf(paramLong)); if (!TextUtils.isEmpty(paramString1)) { localMap.put("articleId", paramString1); } if (!TextUtils.isEmpty(paramString2)) { localMap.put("num_iid", paramString2); } cleanNullParams(localMap); this.markApi.tuiJianGoods(localMap).enqueue(new Callback() {@Override public void onResponse(Call call, Response response) { int i = response.code(); if (i == 200) { MarkRestSource.this.storeToken(((MarkRestResponse.TuijianGoodsResponse) response.body()).token); MarkRestSource.this.dataBus.post(response.body()); } else { MarkRestSource.this.dataBus.post(new MarkRestResponse.TuijianGoodsResponse(Integer.valueOf(i))); } }@Override public void onFailure(Call call, Throwable throwable) { MarkRestSource.this.dataBus.post(new MarkRestResponse.TuijianGoodsResponse(Integer.valueOf(2000))); }}); }
BUS 事件传递
/** * 订阅事件 EventBus @Subscribe * * @return Result< Xxxx> */ @Subscribe public void tuijianGoodsResponse(MarkRestResponse.TuijianGoodsResponse paramTuijianGoodsResponse) { try { if ((paramTuijianGoodsResponse.code == 200) & & (paramTuijianGoodsResponse.data != null) & & paramTuijianGoodsResponse.data.list != null) { this.lastUpDate = paramTuijianGoodsResponse.data.lastUpDate; PageManager localPageManager = this.manager; boolean isLastPage; if (paramTuijianGoodsResponse.data.isLastPage == 0) { isLastPage = false; } else { isLastPage = true; } localPageManager.isLastPage = isLastPage; this.markContentView.refershRecommendItemList(paramTuijianGoodsResponse.data.list); if (TextUtils.isEmpty(this.itemUrl)) { storeMarkrRecords(paramTuijianGoodsResponse.data.list); } } else { this.markContentView.refershRecommendItemList(null); } } catch (Exception ex) { ex.printStackTrace(); } }
【Android MVP 架构一 View与Presenter】
推荐阅读
- win7/10下Qt Creator调试提示(The selected debugger may be inappropriate for the inferior的解决办法)
- android中使用toolbar
- Spring Boot MyBatis注解(@MapperScan和@Mapper)
- 移动端车牌识别为优质app而存在
- 微信跳转外部浏览器下载app
- Android学习之基础知识十三—网络编程的最佳实践
- 设计原理及其重要性
- 因果关系–探索色彩心理学
- 展示设计与视觉叙事艺术