android MVP的使用

1.介绍

M->model:数据

V->view:activity

P->Presenter:连接M和V

2.基本使用

2.1 View层

(1)父类BaseView(IBaseView )

public interface IBaseView {
//    返回错误信息
    void showErrorMsg(String errormsg);
}

(2)子类BaseView(BaseView)

public interface BaseView<T>  extends IBaseView{
//    显示图片,文字
    void showView(List<T> goodsDataList);
//    加载进度条
//    加载动画

}

 2.2 model层

(1) 父类(IBaseModel)

public interface IBaseModel {
    void loadData(OnLoadData onLoadData);
    interface  OnLoadData <T>{
        void onComplete(List<T> goodsDataList);
        void onError(String msg);
    }
}

(2)数据Model(GoodsModel)

public class GoodsModel implements IBaseModel {

    @Override
    public void loadData(OnLoadData onLoadData) {
        onLoadData.onComplete(getData());
    }

    private List<GoodsData> getData() {
        ArrayList goodsList = new ArrayList();
        for (int i = 0; i < 100; i++) {
            GoodsData goodsData = new GoodsData("", "内容1" + i, "内容2" + i);
            goodsList.add(goodsData);
        }
        return goodsList;
    }

}

2.3 Presenter层

(1)父类Presenter(BasePresenter)

public class BasePresenter<T extends IBaseView> {
    WeakReference<T> iGoodView;
    //    绑定
    public void attachView(T view){
        iGoodView=new WeakReference<>(view);

    }
    //    解绑
    public void detachView(){
        if (iGoodView!=null){
            iGoodView.clear();
            iGoodView=null;
        }
    }
}

(2)连接BaseView和GoodsModel的GoodsPresenter

public class GoodsPresenter<T extends IBaseView> extends BasePresenter {

    //    2.持有model接口
    GoodsModel iGoodsModel = new GoodsModel();


    //    3.持有业务逻辑
    public void fetch() {
        if (iGoodView != null && iGoodsModel != null) {
            iGoodsModel.loadData(new IBaseModel.OnLoadData() {
                @Override
                public void onComplete(List goodsDataList) {
                    ((BaseView) iGoodView.get()).showView(goodsDataList);
                }

                @Override
                public void onError(String msg) {

                }
            });
        }
    }
}

3.基础Activity(BaseActivity)

public abstract class BaseActivity<T extends BasePresenter,V extends IBaseView> extends AppCompatActivity {
  protected   T presenter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        布局文件
         setContentView(setLayout());
 //        定义一个选择表示层
        presenter=createPresenter();
        presenter.attachView((V)this);
//        初始化view
        initView();
//        注册
        registerSDK();
//        销毁注册
        unRegisterSDK();
    }

    private void registerSDK() {
    }

    private void unRegisterSDK() {
    }

    protected abstract int setLayout();

    protected abstract void initView();

    protected abstract T createPresenter();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}

4.MainActivity

public class MainActivity extends BaseActivity<GoodsPresenter, IBaseView> implements BaseView {
private ListView lvGoods;
//private GoodsPresenter presenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        lvGoods=findViewById(R.id.lvGoods);
        presenter.fetch();
        lvGoods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected GoodsPresenter createPresenter() {
        return new GoodsPresenter();
    }



    @Override
    public void showView(List goodsDataList) {
        lvGoods.setAdapter(new GoodsAdapter(MainActivity.this,goodsDataList));

    }

    @Override
    public void showErrorMsg(String errormsg) {

    }
}

5.其他界面可以重新写GoodsModel和GoodsPresenter

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值