Some specific charts are not mentioned here! Start the learning journey directly.
1. Basic mvp pattern implementation
An example is to access the web address to get the json string
The website is http://gank.io/api/data/benefits/10/1
The network is implemented by rxjava2+okttp3+retrofit2 Network frame address
First, the layering of mvp
M level
Create an interface ITestModel and an implementation 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 level
Create an interface ITestPresenter and an implementation 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 level
Just create an interface
public interface ITestView {
void onSuccess(List<String> imgs);
void onFail(Throwable throwable);
}
After the MVP structure is created, it can be implemented
public class TestActivity extends AppCompatActivity implements ITestView {
@BindView(R.id.iv_test)
ImageView imageView;
String url = "http://gank.io/api/data/ welfare/“;
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, "Success!", 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, "error!", Toast.LENGTH_SHORT).show();
}
}
The implementation effect and program structure are as follows
Here we implement the mvp loading function!
2.mvp mode general advanced
The above is just to implement a method. Here we can carry out a common advanced step, which is to encapsulate the Activity
First, let's create the following methods
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();
}
After that, we changed the original mvp layered interface and method
P level
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 level
ITestView
public interface ITestView extends IBaseMvpView{
void onSuccess(List<String> imgs);
void onFail(Throwable throwable);
}
Finally, modify TestActivity
public class TestActivity extends MvpActivity<ITestView,TestPresenterImpl> implements ITestView {
@BindView(R.id.iv_test)
ImageView imageView;
String url = "http://gank.io/api/data/benefits/“;
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, "Success!", 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, "error!", Toast.LENGTH_SHORT).show();
}
}
This is a simple advance of mvp
When performing the p-layer operation, just modify the getPresenter() method
summary
At present, I have learned a little. After that, we will continue to study!