具體的一些圖表說明這邊就不提及了!直接開始學(xué)習(xí)之旅。
1.基本mvp模式實現(xiàn)
例子是訪問網(wǎng)址獲取json串
網(wǎng)址是 http://gank.io/api/data/福利/10/1
網(wǎng)絡(luò)使用的是rxjava2+okttp3+retrofit2實現(xiàn)的 網(wǎng)絡(luò)框架地址
首先是mvp的分層
M層
創(chuàng)建一個接口ITestModel和一個實現(xiàn)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層
創(chuàng)建一個接口ITestPresenter和一個實現(xiàn)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層
創(chuàng)建一個接口即可
public interface ITestView {
void onSuccess(List<String> imgs);
void onFail(Throwable throwable);
}
創(chuàng)建好了MVP的結(jié)構(gòu)之后 就可以實現(xiàn)了
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();
}
}
實現(xiàn)的效果和程序結(jié)構(gòu)如下
這邊我們就實現(xiàn)了mvp加載的功能!
2.mvp模式普通進階
上面的就只是實現(xiàn)一個的方法狸捅,這邊我們可以進行普通進階由缆,就是對Activity進行一層封裝
首先我們先創(chuàng)建以下方法
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()方法就好了
總結(jié)
目前就學(xué)了點顽馋。之后的在繼續(xù)深究!