[AS2.3.3]MVP模式学习

具体的一些图表说明这边就不提及了!直接开始学习之旅。


1.基本mvp模式实现

例子是访问网址获取json串
网址是 http://gank.io/api/data/福利/10/1

网络使用的是rxjava2+okttp3+retrofit2实现的 网络框架地址

首先是mvp的分层

M层
创建一个接口ITestModel和一个实现TestModelImpl

public interface ITestModel {
    void getImgs(String url,ITestView view);
}
public class TestModelImpl implements ITestModel{
    @Override
    public void getImgs(String url, final ITestView view) {
        RORManager.getInstance()
                .setUrl(url)
                .create(ITestService.class)
                .getImg(10,1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer() {
                    @Override
                    public void accept(ResponseBody responseBody) throws Exception {
                        String str = responseBody.string();
                        ImgModel imgModel = new Gson().fromJson(str,ImgModel.class);
                        List imgs = new ArrayList<>();
                        for (ImgModel.ResultsBean resultsBean : imgModel.getResults()) {
                            imgs.add(resultsBean.getUrl());
                        }
                        view.onSuccess(imgs);
                    }
                }, new Consumer() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.onFail(throwable);
                    }
                });
    }
}

P层
创建一个接口ITestPresenter和一个实现TestPresenterImpl

public interface ITestPresenter {
    void getImg(String url);
}
public class TestPresenterImpl implements ITestPresenter {
    private ITestView view;
    ITestModel model;

    public TestPresenterImpl(ITestView view){
        this.view = view;
        model = new TestModelImpl();
    }
    @Override
    public void getImg(String url) {
        model.getImgs(url,view);
    }
}

V层
创建一个接口即可

public interface ITestView {
    void onSuccess(List imgs);
    void onFail(Throwable throwable);
}

创建好了MVP的结构之后 就可以实现了

public class TestActivity extends AppCompatActivity implements ITestView {
    @BindView(R.id.iv_test)
    ImageView imageView;

    String url = "http://gank.io/api/data/福利/";
    private int s = 0;
    private List imgs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        ButterKnife.bind(this);

        TestPresenterImpl presenter = new TestPresenterImpl(this);
        presenter.getImg(url);
    }

    @OnClick(R.id.btn_test)
    public void click(){
        s++;
        if (s > 9) {
            s = 0;
        }
        setImg(imgs.get(s));
    }

    @Override
    public void onSuccess(List imgs) {
        Toast.makeText(this, "成功!", Toast.LENGTH_SHORT).show();
        this.imgs = imgs;
        setImg(imgs.get(0));
    }

    private void setImg(String s) {
        Glide.with(this).load(s).into(imageView);
    }

    @Override
    public void onFail(Throwable throwable) {
        Toast.makeText(this, "错误!", Toast.LENGTH_SHORT).show();
    }
}

实现的效果和程序结构如下


[AS2.3.3]MVP模式学习_第1张图片
1.gif

[AS2.3.3]MVP模式学习_第2张图片
程序结构

这边我们就实现了mvp加载的功能!


2.mvp模式普通进阶

上面的就只是实现一个的方法,这边我们可以进行普通进阶,就是对Activity进行一层封装
首先我们先创建以下方法


[AS2.3.3]MVP模式学习_第3张图片
新泛型
public interface IBaseMvpView {
}
public interface IBaseMvpPresenter {
    void onAttached(V view);
    void onDetached();
    boolean isAttached();
    V getMvpView();
}
public class BasePresenter implements IBaseMvpPresenter {
    private V mvpView;
    @Override
    public void onAttached(V view) {
        mvpView = view;
    }

    @Override
    public void onDetached() {
        mvpView = null;
    }

    @Override
    public boolean isAttached() {
        return mvpView != null;
    }

    @Override
    public V getMvpView() {
        return mvpView;
    }
}
public abstract class MvpActivity> extends AppCompatActivity {

    protected P mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = getPresenter();
        if (mPresenter == null) {
            throw new NullPointerException("presenter is null.");
        }
        mPresenter.onAttached((V) this);
    }

    @Override
    protected void onDestroy() {
        if (mPresenter != null) {
            mPresenter.onDetached();
        }
        super.onDestroy();
    }

    protected abstract P getPresenter();
}

之后我们对原来的mvp分层的接口和方法进行改造
P层
TestPresenterImpl

public class TestPresenterImpl extends BasePresenter implements ITestPresenter {
    ITestModel model;

    public TestPresenterImpl(){
        model = new TestModelImpl();
    }
    @Override
    public void getImg(String url) {
        model.getImgs(url,getMvpView());
    }
}

V层
ITestView

public interface ITestView extends IBaseMvpView{
    void onSuccess(List imgs);
    void onFail(Throwable throwable);
}

最后修改TestActivity

public class TestActivity extends MvpActivity implements ITestView {
    @BindView(R.id.iv_test)
    ImageView imageView;

    String url = "http://gank.io/api/data/福利/";
    private int s = 0;
    private List imgs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        ButterKnife.bind(this);
        mPresenter.getImg(url);
    }

    @Override
    protected TestPresenterImpl getPresenter() {
        return new TestPresenterImpl();
    }

    @OnClick(R.id.btn_test)
    public void click(){
        s++;
        if (s > 9) {
            s = 0;
        }
        setImg(imgs.get(s));
    }

    @Override
    public void onSuccess(List imgs) {
        Toast.makeText(this, "成功!", Toast.LENGTH_SHORT).show();
        this.imgs = imgs;
        setImg(imgs.get(0));
    }

    private void setImg(String s) {
        Glide.with(this).load(s).into(imageView);
    }

    @Override
    public void onFail(Throwable throwable) {
        Toast.makeText(this, "错误!", Toast.LENGTH_SHORT).show();
    }
}

这样算是对mvp进行了一次简单的进阶

进行p层操作的时候只要修改getPresenter()方法就好了


总结

目前就学了点。之后的在继续深究!

你可能感兴趣的