MVP 设计模式.
Model
主要负责数据交互.
实现数据请求方法,使用回调接口.
1 2 3 4 5 6 7 8 9 10
| public class MainModel{ private Context context; public MainModel(Context context){ this.context = context; } public void getGankList(String date, CallBack callBank){ //网络请求,数据返回给 callBank } }
|
View
定义接口. 操作各种控件.
1 2 3 4 5
| public interface MainView{ public void setData(List list); public void showToast(String msg); public Context getContext(); }
|
Presenter
负责Model和View的交互. 持有Model和View的引用, 调用Model中的接口,在回调中调用View中的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class MainPresenter{ private MainView view; private MainModel model; public MainPresenter(MainView view){ this.view = view; model = new MainModel(view.getContext); } public getGankList(String date){ model.getGankList(date, new CallBack(){ public void onSuccess(List list){ view.setData(list); } }) } }
|
Activity
实现 View 接口
持有 Presenter 的引用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class MainActivity extends BaseActivity implements MainView{ private MainPresenter presenter; @Override public void onCreate(){ presenter = new MainPresenter(this); presenter.getGankList("2017-02-21"); } @Override public void setData(List list){ //给控件赋值.. } @Override public void getContext{ return this; } }
|