谈起MVP,有点惭愧,还是等到面试的时候被人问到才去做了解,自己阅读了网上的一部分MVP文档,感觉每个人都是按照自己的理解方式来写自己的博客,自己也写个自己的心得体会。
网上的图一大堆,这里就截取一个给大家看。
大神鸿杨的总结就是:Model与View之间的交互由Presenter完成。还有一点就是Presenter与View之间的交互是通过接口的(代码中会体现)。
谈起例子也是各有不同。
下面是我的例子,首先给大家看一下结构:
对应的类如下:
TestPresentBean
1 public class TestPresentBean { 2 public String name; 3 public String password; 4 5 public String getName() { 6 return name; 7 } 8 9 public void setName(String name) { 10 this.name = name; 11 } 12 13 public String getPassword() { 14 return password; 15 } 16 17 public void setPassword(String password) { 18 this.password = password; 19 } 20 }
ITestPresentView
public interface ITestPresentView { void findView(); void showLoading(); void dismissLoading(); void showUserInfo(TestPresentBean bean); }
TestPresentModel
public class TestPresentModel { public void loadData(final String name, final String pass, final ListenCallBack listenCallBack){ new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } TestPresentBean bean = new TestPresentBean(); bean.setName(name); bean.setPassword(pass); listenCallBack.loadSuccess(bean); } }).start(); } }
TestPresenter
public class TestPresenter { Handler mHandler = new Handler(); ITestPresentView iTestPresentView; TestPresentModel testPresentbiz; public TestPresenter(ITestPresentView iTestPresentView) { this.iTestPresentView = iTestPresentView; testPresentbiz = new TestPresentModel(); } public void loadData(){ iTestPresentView.showLoading(); testPresentbiz.loadData("张三", "123", new ListenCallBack() { @Override public void loadSuccess(final TestPresentBean testPresentBean) { //需要在UI线程执行 mHandler.post(new Runnable() { @Override public void run() { iTestPresentView.showUserInfo(testPresentBean); iTestPresentView.dismissLoading(); } }); } @Override public void loadFailed() { //需要在UI线程执行 mHandler.post(new Runnable() { @Override public void run() { iTestPresentView.dismissLoading(); } }); } }); } }
TestPresentActivity
public class TestPresentActivity extends AppCompatActivity implements ITestPresentView { TestPresenter presenter; TextView mTestView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_present); presenter = new TestPresenter(this); initView(); initData(); } private void initView() { mTestView = (TextView) findViewById(R.id.txt_show); } private void initData() { TestPresenter presenter = new TestPresenter(this); presenter.loadData(); } @Override public void findView() { } @Override public void showLoading() { Toast.makeText(this, "正在加载", Toast.LENGTH_LONG).show(); } @Override public void dismissLoading() { Toast.makeText(this, "加载完成", Toast.LENGTH_LONG).show(); } @Override public void showUserInfo(TestPresentBean bean) { mTestView.setText(bean.getName()+":"+bean.getPassword()); } }
每个人有自己的用法,希望这些跟您自己的代码作对比,写出更规范的MVP模式。
这里给大家提供一些参考网站:
http://www.jianshu.com/p/14283d8d3a60
http://www.jianshu.com/p/9a6845b26856
http://blog.csdn.net/lmj623565791/article/details/46596109
http://blog.csdn.net/hanchendong/article/details/61919599