android -------- MVP+DataBinding 的使用
今天来说说MVP+DataBinding 的使用
以一个登录案例来讲解

布局:(ConstraintLayout 作为根布局)
<layout>
<data>
<variable
name="onClick"
type="com.zhangqie.mvplogin.LoginActivity.OnViewClick" />
</data>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:gravity="center"
android:text="账号:"
android:textColor="@android:color/black"
android:textSize="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4" />
<EditText
android:id="@+id/et_name"
android:layout_width="222dp"
android:layout_height="45dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/tv1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:gravity="center"
android:text="密码:"
android:textColor="@android:color/black"
android:textSize="16dp"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv1" />
<EditText
android:id="@+id/et_pwd"
android:layout_width="222dp"
android:layout_height="45dp"
app:layout_constraintLeft_toRightOf="@+id/tv2"
app:layout_constraintTop_toBottomOf="@+id/et_name" />
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="25dp"
android:onClick="@{onClick.OnClickCommand}"
android:text="登录"
app:layout_constraintTop_toBottomOf="@+id/et_pwd" />
</android.support.constraint.ConstraintLayout>
</layout>
BaseActivity.Java
public abstract class BaseActivity<D extends ViewDataBinding,V,T extends BasePresenter<V>> extends AppCompatActivity{
protected D viewDataBinding;
protected T p;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewDataBinding = DataBindingUtil.setContentView(this, setMainLayout());
p = createPresenter();
p.attachView((V)this);
initView();
initBeforeData();
}
protected abstract T createPresenter();
/***
* 初始化布局
*/
protected abstract int setMainLayout();
/**
* 初始化View
*/
protected abstract void initView();
/**
* 初始化先前数据
*/
protected abstract void initBeforeData();
/***
* 跳转Activity
* @param mClass
*/
protected void openActivity(Class<?> mClass) {
openIntent(new Intent(this, mClass));
}
/**
* 弹出toast 显示时长short
*
* @param msg
*/
protected void showToastShort(String msg) {
if (!TextUtils.isEmpty(msg)) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}
protected void showToastShort(int msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
protected void openIntent(Intent intent) {
startActivity(intent);
}
protected void openForResultActivity(Intent intent, int requestCode){
startActivityForResult(intent,requestCode);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (p != null){
p.detachView();
}
}
}
Activity.java
public class LoginActivity extends BaseActivity<LoginMainBinding,IView,LoginPresenter> implements IView {
@Override
protected LoginPresenter createPresenter() {
return new LoginPresenter();
}
@Override
protected int setMainLayout() {
return R.layout.login_main;
}
@Override
protected void initView() {
viewDataBinding.setOnClick(new OnViewClick());
}
@Override
protected void initBeforeData() {
}
@Override
public void showLoading(String msg) {
showToastShort(msg);
}
public class OnViewClick {
public void OnClickCommand(View view) {
switch (view.getId()) {
case R.id.btn_login:
p.showLogin(viewDataBinding.etName.getText().toString(),viewDataBinding.etPwd.getText().toString());
break;
}
}
}
}
效果图:

源码下载: https://github.com/DickyQie/android-databinding
总结:
减少各层之间耦合,易于后续的需求变化,降低维护成本。
Presenter层独立于Android代码之外,可以进行Junit测试。
接口和类较多,互相做回调,代码臃肿。
Presenter层与View层是通过接口进行交互的,接口粒度不好控制。
有不足之处,望指正
android -------- MVP+DataBinding 的使用的更多相关文章
- Android MVP+Retrofit+RxJava实践小结
关于MVP.Retrofit.RxJava,之前已经分别做了分享,如果您还没有阅读过,可以猛戳: 1.Android MVP 实例 2.Android Retrofit 2.0使用 3.RxJava ...
- [Android]Android MVP&依赖注入&单元测试
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5422443.html Android MVP&依赖注入 ...
- Android MVP + 泛型,实现了友好VP交互及Activity潜在的内存泄露的优化
Android MVP粗来已经有段时间了,在项目中我也多多少少用了一些,不得不说代码使用这种模式后,条例确实清晰了好多,整个流程看起来有点各司其职的感觉(另一种的java面向对象的方式). 不过这里是 ...
- android MVP模式介绍与实战
android MVP模式介绍与实战 描述 MVP模式是什么?MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数 ...
- Android MVP模式
转自http://segmentfault.com/blogs,转载请注明出处Android MVP Pattern Android MVP模式\[1\]也不是什么新鲜的东西了,我在自己的项目里也普遍 ...
- Android MVP模式 简单易懂的介绍方式
主要学习这位大神的博客:简而易懂 Android MVP模式 简单易懂的介绍方式 https://segmentfault.com/a/1190000003927200
- 结合实例分析Android MVP的实现
最近阅读项目的源码,发现项目中有MVP的痕迹,但是自己却不能很好地理解相关的代码实现逻辑.主要原因是自己对于MVP的理解过于概念话,还没有真正操作过.本文打算分析一个MVP的简单实例,帮助自己更好的理 ...
- 浅谈Android MVP
什么是MVP MVP,全称 Model-View-Presenter.要说MVP那就不得不说一说它的前辈--MVC(Model-View-Controller,模型-视图-控制器). View:对应于 ...
- Android MVP模式简单易懂的介绍方式 (三)
Android MVP模式简单易懂的介绍方式 (一) Android MVP模式简单易懂的介绍方式 (二) Android MVP模式简单易懂的介绍方式 (三) 讲完M和P,接下来就要讲V了.View ...
随机推荐
- SIM800C 连接服务器
AT+CIPSTART=TCP,域名,端口号 OK 只返回OK,这种情况,说明域名的服务器出错了,OK表示格式正确,但是实际上的TCP是没有连接上的. 测试库服务器出错的时候,就是这种情况 实际连上了 ...
- apache tomcat (catalina)查版本(solaris/unix)
先进到tomcat的bin目录下(cd /tomcat目录/bin),在执行./version.sh https://blog.csdn.net/vv___/article/details/78653 ...
- 20144306《网络对抗》MAL_恶意代码分析
一.基础问题 1.如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些,用什么方法来监控? 使用Windows自带的schtasks指 ...
- ES6 字符串
拓展的方法 子串的识别 ES6 之前判断字符串是否包含子串,用 indexOf 方法,ES6 新增了子串的识别方法. includes():返回布尔值,判断是否找到参数字符串. startsWith( ...
- vsftp快速搭建ftp服务器
什么是vsftp: vsftpd 是一个 UNIX 类操作系统上运行的服务器的名字,它可以运行在诸如 Linux, BSD, Solaris, HP-UX 以及 IRIX 上面.它支持很多其他的 FT ...
- 身份证运算符 is 和 is not(检查两个数据在内存当中是否是同一个值) | 逻辑运算符 and or not | 数据类型的判断 isinstance
# ###身份证运算符 is 和 is not(检查两个数据在内存当中是否是同一个值) var1 = 6 var2 = 6 print(id(var1),id(var2)) var1 = " ...
- Centos下,Docker部署Yapi接口管理平台(详细得令人发指)
接口测试的工具很多,公司引进了接口管理平台Yapi,自己尝试直接搭建,从安装Nodejs到配置MongoDB数据库,再到安装yapi的时候,遇到浏览器打开本地服务器Ip地址后,没有显示部署内容...没 ...
- 【LeetCode每天一题】Spiral Matrix II(螺旋数组II)
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral ord ...
- poj1733(并查集+离散化)
题目大意:有一个长度为n的0,1字符串, 给m条信息,每条信息表示第x到第y个字符中间1的个数为偶数个或奇数个, 若这些信息中第k+1是第一次与前面的话矛盾, 输出k; 思路:x, y之间1的个数为偶 ...
- Array 转 Set
Array 转 Set: Set<String> oldCandidateNames = new HashSet<String>(Arrays.asList(candidate ...