Android MVP 架构一 View与Presenter

Posted hoge

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

Android应用架构之Android MVP使用

Android Mvp架构的小demo

android MVP——mvp架构的应用和优化

MVP架构-Android官方MVP项目和响应式MVP-RxJava项目架构分析对比解读

Android MVP架构模式

Android 中 MVC、MVP 和 MVVM 对比