为什么是浅尝辄止呢?应为google正在酝酿mvvm框架如databiding,所以有必要等一等
对于android mvp架构,实在mvc 上面演化而来,回顾一下android mvc:
View:对应于布局文件xml
model: 模型javabean
control:Activity或者fragment
稍微有过开发经验的人都知道,activity也承担了大部分view的角色,activity在control控制层时 有时候一个activity的代码量可能超过1000行,逻辑混乱,不易维护,担任的任务太重
这时候mvp就是这样演变出来 将view和activity相对隔离:用链接者presenter 他的作用就是负责view和model之间的交互,这样view和model不直接交互,这样相对更加自由
实例:
典型的javaBean
package com.xuan.androidmvp.bean; /** * @author xuanyouwu * @email [email protected] * @time 2016-04-20 10:37 */ public class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } public User() { } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.xuan.androidmvp.model; import com.xuan.androidmvp.bean.User; /** * @author xuanyouwu * @email [email protected] * @time 2016-04-20 11:05 */ public interface IUserModel { void setName(String name); void setAge(int age); User getUserInfo(int id); void setId(int id); int getId(); }
package com.xuan.androidmvp.model.imp; import com.xuan.androidmvp.bean.User; import com.xuan.androidmvp.model.IUserModel; /** * @author xuanyouwu * @email [email protected] * @time 2016-04-20 11:09 */ public class UserModel implements IUserModel { @Override public void setName(String name) { } @Override public void setAge(int age) { } @Override public User getUserInfo(int id) { // 从存储中获取 // return null; return new User("张三", 26); } @Override public void setId(int id) { } @Override public int getId() { return 0; } }
</pre><pre code_snippet_id="1654150" snippet_file_name="blog_20160420_5_9679489" name="code" class="java">现在就轮到连接器或者主持人吧
</pre><pre code_snippet_id="1654150" snippet_file_name="blog_20160420_7_7444235" name="code" class="java"><pre name="code" class="java">package com.xuan.androidmvp.presenter; import com.xuan.androidmvp.bean.User; import com.xuan.androidmvp.model.IUserModel; import com.xuan.androidmvp.model.imp.UserModel; import com.xuan.androidmvp.view.IUserView; /** * @author xuanyouwu * @email [email protected] * @time 2016-04-20 11:02 */ public class UserParesenter { private IUserView userView; private IUserModel userModel; public UserParesenter(IUserView userView) { this.userView = userView; this.userModel = new UserModel(); } public void saveUser(int id, String name, int age) { this.userModel.setId(id); this.userModel.setName(name); this.userModel.setAge(age); } public void loadUser(int id) { User userInfo = userModel.getUserInfo(id); userView.setName(userInfo.getName()); userView.setAge(userInfo.getAge()); } }
</pre><pre code_snippet_id="1654150" snippet_file_name="blog_20160420_9_1031503" name="code" class="java">UI:Activity:
</pre><pre code_snippet_id="1654150" snippet_file_name="blog_20160420_11_8239839" name="code" class="html"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.xuan.androidmvp.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="name" /> <EditText android:id="@+id/et_age" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="age" /> <Button android:id="@+id/bt_ok" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="保存" /> <View android:layout_width="match_parent" android:layout_height="10dp" android:background="#6cf" /> <TextView android:id="@+id/tv_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="姓名:" android:padding="10dp" /> <TextView android:id="@+id/tv_age" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="年龄:" android:padding="10dp" /> </LinearLayout> </RelativeLayout>
package com.xuan.androidmvp; import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import com.xuan.androidmvp.presenter.UserParesenter; import com.xuan.androidmvp.view.IUserView; public class MainActivity extends AppCompatActivity implements IUserView { UserParesenter paresenter; EditText et_name, et_age; TextView tv_name, tv_age; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_name = (EditText) findViewById(R.id.et_name); et_age = (EditText) findViewById(R.id.et_age); tv_name = (TextView) findViewById(R.id.tv_name); tv_age = (TextView) findViewById(R.id.tv_age); findViewById(R.id.bt_ok).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int uId = (int) SystemClock.elapsedRealtime(); paresenter.saveUser(uId, getName(), getAge()); paresenter.loadUser(uId); } }); paresenter = new UserParesenter(this); } @Override public String getName() { return et_name.getText().toString(); } @Override public int getAge() { return Integer.valueOf(et_age.getText().toString()); } @Override public void setName(String name) { tv_name.setText(name); } @Override public void setAge(int age) { tv_age.setText(Integer.toString(age)); } }