什么是MVP呢?它又和我们常常听到的MVC有什么关系了以及区别呢?
ProgressBar控件和TextView控件,我们判断它是否有网络连接,如果有的话就隐藏 ProgressBar和跳转到MainActivity如果没有网络的话则显示ProgressBar和TextView,TextView则提示用户No internet。就这么简单的一个需求,我们看看如何用MVP模式做这个需求
首先我们看下M层是如何接口写的
<span style="font-size:18px;">package com.manning.androidhacks.hack020.presenter.model; public interface IConnectionStatus { boolean isOnline(); } </span>然后看看实现(我们主要是看MVP模式的使用,所以在此就不做网络连接的检查了,模拟一个状态)
<span style="font-size:18px;">package com.manning.androidhacks.hack020.presenter.model.impl; import com.manning.androidhacks.hack020.presenter.model.IConnectionStatus; public class ConnectionStatus implements IConnectionStatus { @Override public boolean isOnline() { // TODO: Here we should place the code to check the connectivity. return true; } }</span>然后我们在来看看M的接口
<span style="font-size:18px;">package com.manning.androidhacks.hack020.view; public interface ISplashView { void showProgress(); void hideProgress(); void showNoInetErrorMsg(); void moveToMainView(); } </span>以及M的实现
<span style="font-size:18px;">package com.manning.androidhacks.hack020.view.impl; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; import com.manning.androidhacks.hack020.R; import com.manning.androidhacks.hack020.presenter.SplashPresenter; import com.manning.androidhacks.hack020.view.ISplashView; public class SplashActivity extends Activity implements ISplashView { private TextView mTextView; private ProgressBar mProgressBar; private SplashPresenter mPresenter = new SplashPresenter(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); mPresenter.setView(this); mTextView = (TextView) findViewById(R.id.splash_text); mProgressBar = (ProgressBar) findViewById(R.id.splash_progress_bar); } @Override protected void onResume() { super.onResume(); mPresenter.didFinishLoading(); } public void showProgress() { mProgressBar.setVisibility(View.VISIBLE); } public void hideProgress() { mProgressBar.setVisibility(View.INVISIBLE); } public void showNoInetErrorMsg() { mTextView.setText("No internet"); } @Override public void moveToMainView() { startActivity(new Intent(this, MainActivity.class)); } }</span>
最后我们来看看P层是如何控制他们的逻辑的:
<span style="font-size:18px;">package com.manning.androidhacks.hack020.view.impl; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; import com.manning.androidhacks.hack020.R; import com.manning.androidhacks.hack020.presenter.SplashPresenter; import com.manning.androidhacks.hack020.view.ISplashView; public class SplashActivity extends Activity implements ISplashView { private TextView mTextView; private ProgressBar mProgressBar; private SplashPresenter mPresenter = new SplashPresenter(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); mPresenter.setView(this); mTextView = (TextView) findViewById(R.id.splash_text); mProgressBar = (ProgressBar) findViewById(R.id.splash_progress_bar); } @Override protected void onResume() { super.onResume(); mPresenter.didFinishLoading(); } public void showProgress() { mProgressBar.setVisibility(View.VISIBLE); } public void hideProgress() { mProgressBar.setVisibility(View.INVISIBLE); } public void showNoInetErrorMsg() { mTextView.setText("No internet"); } @Override public void moveToMainView() { startActivity(new Intent(this, MainActivity.class)); } }</span>
ym——Android开发MVP模式(解决了View和Model的耦合)
原文地址:http://blog.csdn.net/cym492224103/article/details/38776873