[AS2.3.3] MVP mode learning and as2.3.3mvp mode Learning
Some specific charts are not mentioned here! Start your learning journey directly.
1. Basic mvp mode implementation
The example is to access the URL to obtain the json string.
The network uses the network framework address implemented by rxjava2 + okttp3 + javasfit2.
The first is the mvp hierarchy.
M layer
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
() { @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 Layer
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 Layer
Create an interface.
public interface ITestView { void onSuccess(List
imgs); void onFail(Throwable throwable);}
After creating the MVP structure, you can implement it.
Public class TestActivity extends AppCompatActivity implements ITestView {@ BindView (R. id. iv_test) ImageView imageView; String url = "https://gank.io/api/data/welfare/"; 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, "successful! ", 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 have implemented the mvp loading function!
2. mvp mode General advanced
The above is just a method to implement. Here we can perform general advanced operations, that is, encapsulate the Activity layer.
First, create the following methods:
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();}
Then we will transform the interface and method of the old mvp hierarchy.
P Layer
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 Layer
ITestView
public interface ITestView extends IBaseMvpView{ void onSuccess(List
imgs); void onFail(Throwable throwable);}
Last modified TestActivity
Public class TestActivity extends MvpActivity
Implements ITestView {@ BindView (R. id. iv_test) ImageView imageView; String url = "https://gank.io/api/data/welfare/"; 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, "successful! ", 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 and advanced process for mvp members.
You only need to modify the getPresenter () method when performing the p-layer operation.