Android MVP 架构一 View与Presenter
View:主要負責(zé)界面的顯示及跟數(shù)據(jù)無關(guān)的邏輯,比如設(shè)置控件的點擊事件等
Presenter:主要負責(zé)View與Model的交互
Model:數(shù)據(jù)部分
-------
MVP的核心是:
View層不持有Model層對象任何引用,當(dāng)然參數(shù)里面和臨時變量里可以有Model層對象,只持有Presenter層對象引用,任何需要更新或者操作數(shù)據(jù)的,都間接通過Presenter對象去操作數(shù)據(jù)。而Model層想要操作View層是無法實現(xiàn)的,必須通過Presenter層。
Presenter層持有View層對象的引用,除此之外不持有其他的UI控件等的引用,Model層會把想要更新View的操作委托Presenter去操作,而Presenter層會把更新View操作交給View層對象去操作。
-------
1、view 層,實現(xiàn):
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 中實現(xiàn)上面的方法:
@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、請求服務(wù)器:
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();
}
}
www.beicaiduo.com
總結(jié)
以上是生活随笔為你收集整理的Android MVP 架构一 View与Presenter的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DIV使用tabindex获得事件详解
- 下一篇: 斐波那契fib