一、什么是MVP
MVP:全称 Model-View-Presenter。
MVP框架由3部分组成:View层负责显示,Presenter层负责逻辑处理,Model层提供数据。
View:负责绘制UI元素、与用户进行交互(在Android中体现为Activity,Fragment)
Model:负责存储、检索、操纵数据
Presenter:作为View与Model交互的中间纽带,处理与用户交互的负责逻辑。
image.png
image.png二、创建Model Presenter View三个包
三、代码编写
1.View层代码
view层负责界面操作,如何确定view层里面有那些操作呢?
拿我这个UserView举例.
onLogin代表登录时候要做的事情,比如显示一个进度dialog
loginSuccess代表登录成功后要做的事情,做界面跳转等操作
loginFail表示登录失败后得操作,显示一个Toast等
public interface UserView{
void onLogin();
void loginSuccess(UserBean bean);
void loginFail(Exception e,int code);
}
2.Presenter层代码
这层主要负责View层和Model层的连接,也有中间人得意思,
View层请求Presenter层,Presenter在请求Model层,Model层处理数据之后通过回调接口传递给Presenter层,Presenter在传递给View层
在上一张图
image.png
public class UserPresenter implements UserCallback {
private UserView mUserView;
private UserModel mModel;
public UserPresenter(UserView baseView) {
mUserView = baseView;
mModel= new UserModel(this);
}
public void userLogin(String name, String pass){
mUserView.onLogin();
mModel.userLogin(name,pass);
}
@Override
public void onSuccess(UserBean bean) {
mUserView.loginSuccess(bean);
}
@Override
public void onFail(Exception e, int code) {
mUserView.loginFail(e,code);
}
}
public interface UserCallback {
void onSuccess(UserBean bean);
void onFail(Exception e ,int code);
}
3.model层代码
model层进行网络请求等耗时任务处理,model会留有一个回调地址,当数据处理完成之后回调它,在本实例中UserCallback就是一个回调接口,具体实现类在Presenter层,也就是说数据处理完成之后会传递给Presenter层
public class UserModel {
private UserCallback mUserCallback;
private Map<String,String> mUserMaps;
private Handler mHandler;
/**
*
* @param userCallback 回调地址,当数据处理完成之后调用这个接口
*/
public UserModel(UserCallback userCallback) {
mUserCallback = userCallback;
mUserMaps=new HashMap<>();
mHandler =new Handler(Looper.getMainLooper());
mUserMaps.put("user1","123456");
mUserMaps.put("user2","12345");
mUserMaps.put("user3","1234");
}
public void userLogin(final String name, final String pass){
/**
* 模拟延时操作,2秒后调用UserCallback接口,回传数据
*/
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (mUserMaps.containsKey(name) && mUserMaps.get(name).equals(pass)){
UserBean bean =new UserBean(name,pass,20);
mUserCallback.onSuccess(bean);
}else {
Exception exception =new Exception("用户名或账号错误");
mUserCallback.onFail(exception,400);
}
}
},2000);
}
}
4.MainActivity
public class MainActivity extends AppCompatActivity implements UserView ,View.OnClickListener {
private ProgressDialog mProgressDialog;
private EditText mUserText;
private EditText mUserPass;
private Button mLoginBtn;
private UserPresenter mUserPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUserPresenter=new UserPresenter(this);
initView();
initEvents();
}
private void initEvents() {
mLoginBtn.setOnClickListener(this);
}
public void initView () {
mUserText = (EditText) findViewById(R.id.userName);
mUserPass = (EditText) findViewById(R.id.userPass);
mLoginBtn = (Button) findViewById(R.id.loginBtn);
}
@Override
public void onLogin() {
mProgressDialog =new ProgressDialog(this);
mProgressDialog.show();
}
@Override
public void loginSuccess(UserBean bean) {
Toast.makeText(this, "登录成功,年龄"+bean.getAge(), Toast.LENGTH_SHORT).show();
}
@Override
public void loginFail(Exception e, int code) {
if (mProgressDialog!=null){
mProgressDialog.dismiss();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.loginBtn:
if (getUserPass().length()==0 || getUserText().length()==0){
Toast.makeText(this, "请输入账号或密码", Toast.LENGTH_SHORT).show();
return;
}
mUserPresenter.userLogin(getUserText(),getUserPass());
break;
}
}
private String getUserText(){
return mUserText.getText().toString();
}
private String getUserPass(){
return mUserPass.getText().toString();
}
}
5.XML布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#ffffff"
tools:context=".MainActivity">
<LinearLayout
android:layout_margin="15dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:textColor="#616161"
android:id="@+id/userName"
android:inputType="text"
android:background="@drawable/shape_editview"
android:layout_width="match_parent"
android:layout_height="55dp" />
<EditText
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:textColor="#616161"
android:layout_marginTop="8dp"
android:id="@+id/userPass"
android:inputType="textPassword"
android:background="@drawable/shape_editview"
android:layout_width="match_parent"
android:layout_height="55dp" />
<Button
android:layout_marginTop="8dp"
android:textColor="#ffffff"
android:textSize="19sp"
android:background="@drawable/shape_login_btn"
android:text="登录"
android:id="@+id/loginBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
image.png
6.两个shape
shape_editview.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="30dp"></corners>
<solid android:color="#f7f5f5"></solid>
</shape>
shape_login_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<corners android:radius="100dp"></corners>
<solid android:color="#03a9f4"></solid>
</shape>
</item>
<item android:state_pressed="false">
<shape>
<corners android:radius="100dp"></corners>
<solid android:color="#0a9ddf"></solid>
</shape>
</item>
</selector>
以上就是MVP模式得简单实现.一定要多加练习,才能体会掉MVP得优点
网友评论