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 ...
随机推荐
- SQLAlchemy(包含有Flask-Migrate知识点)
what's the SQLAlchemy SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言之便是:将类和对象转换成SQ ...
- 分布式异步任务Celery
-A代表APP celery -A tasks worker --loglevel=info -n nodemaster -------------- celery@nodemaster v4.1.0 ...
- WinDBG相关
WinDBG 技巧:如何生成Dump 文件(.dump 命令) 程序崩溃(crash)的时候, 为了以后能够调试分析问题, 可以使用WinDBG要把当时程序内存空间数据都保存下来,生成的文件称为d ...
- cocos2d-x 贡献一个oss上传脚本
平常写前端项目和H5游戏时特别频繁的一个操作就是上传到oss上,特别浪费时间.所以用ali-oss写了一个脚本.配置属性后直接npm run oss就能上传到oss上了.再也不需要手动操作.现在是脚本 ...
- CSS 字体效果
text-shadow还没有出现时,大家在网页设计中阴影一般都是用photoshop做成图片,现在有了css3可以直接使用text-shadow属性来指定阴影.这个属性可以有两个作用,产生阴影和模糊主 ...
- SVN拉分支,合并分支
前提是:本地已安装SVN,且在SVN中新建好branch和tag目录 拉分支: 把svn内容下载到本地,然后右键TortoiseSVN-->Branch/tag... 上图中红框选择存放的路径, ...
- Python Logging模块 输出日志颜色、过期清理和日志滚动备份
# coding:utf-8 import logging from logging.handlers import RotatingFileHandler # 按文件大小滚动备份 import co ...
- The Code analysis of the FFDNet model
1. 读取图像并判断是否为灰度图,如为RGB图转化为灰度图,并读取图像的w.h 2.数据格式转换:将uint8表示的读取图像矩阵变为double表示. 3.加入噪声,如果噪声水平$\sigma = 5 ...
- mysql插入数据报错1366
数据表插入中文数据报错 Warning Code : 1366 Incorrect string value: '\xE5\x9C\xA8' for column 'name' at row 1 原因 ...
- 3、Kafka集群部署
Kafka集群部署 1)解压安装包 [ip101]$ tar -zxvf kafka_2.11-0.11.0.0.tgz -C /opt/app/ 2)修改解压后的文件名称 [ip101]$ mv k ...