具体的一些图表说明这边就不提及了!直接开始学习之旅。
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<ResponseBody>() {
@Override
public void accept(ResponseBody responseBody) throws Exception {
String str = responseBody.string();
ImgModel imgModel = new Gson().fromJson(str,ImgModel.class);
List<String> imgs = new ArrayList<>();
for (ImgModel.ResultsBean resultsBean : imgModel.getResults()) {
imgs.add(resultsBean.getUrl());
}
view.onSuccess(imgs);
}
}, new Consumer<Throwable>() {
@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<String> 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<String> 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<String> 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();
}
}
实现的效果和程序结构如下
1.gif
程序结构
这边我们就实现了mvp加载的功能!
2.mvp模式普通进阶
上面的就只是实现一个的方法,这边我们可以进行普通进阶,就是对Activity进行一层封装
首先我们先创建以下方法
新泛型
public interface IBaseMvpView {
}
public interface IBaseMvpPresenter<V extends IBaseMvpView> {
void onAttached(V view);
void onDetached();
boolean isAttached();
V getMvpView();
}
public class BasePresenter<V extends IBaseMvpView> implements IBaseMvpPresenter<V> {
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<V extends IBaseMvpView,P extends IBaseMvpPresenter<V>> 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<ITestView> 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<String> imgs);
void onFail(Throwable throwable);
}
最后修改TestActivity
public class TestActivity extends MvpActivity<ITestView,TestPresenterImpl> implements ITestView {
@BindView(R.id.iv_test)
ImageView imageView;
String url = "http://gank.io/api/data/福利/";
private int s = 0;
private List<String> 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<String> 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()方法就好了
总结
目前就学了点。之后的在继续深究!
网友评论