一、概念

关于MVC、MVP与MVVM的概念就不介绍了,总之一句话,MVVM概念出现比MVP早,MVP比MVC早,作为程序员就应该去学习最新的技术不是?详细的概念介绍移步这里吧,https://www.jianshu.com/p/4830912f5162

二、MVVM的使用办法

第一步:在工程的build.gradle中配置

     dataBinding{
enabled = true
}

第二步:修改布局文件,记得布局文件一定要用layout标签给括起来,下面先把布局文件都贴出来

 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"> <data>
<variable
name="user"
type="com.plbear.doncal.rxjavademo.User"></variable> <variable
name="clickHandler"
type="com.plbear.doncal.rxjavademo.MainActivity.ClickHandler"></variable>
</data> <android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:id="@+id/lab_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="63dp"
android:layout_marginStart="63dp"
android:layout_marginTop="46dp"
android:text="@{user.name}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" /> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="63dp"
android:layout_marginStart="63dp"
android:layout_marginTop="26dp"
android:onClick="@{clickHandler.btnClickHandler}"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> <TextView
android:id="@+id/lab_passwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:text="@{user.passwd}"
app:layout_constraintStart_toStartOf="@+id/lab_name"
app:layout_constraintTop_toBottomOf="@+id/lab_name" /> </android.support.constraint.ConstraintLayout>
</layout>

从这个布局文件中,我们看到定义了两个变量,分别是user和clickHandler,其中,在两个TextView文件中,分别用以下的办法来来使用:

 android:text="@{user.name}"
 android:onClick="@{clickHandler.btnClickHandler}"

第二步:Java文件的修改

新增一个User数据类

 public class User {
public ObservableField<String> name = new ObservableField<>();
public ObservableField<String> passwd = new ObservableField<>();
}

MainActivity的代码及解释如下:

 package com.plbear.doncal.rxjavademo;

 import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; import com.plbear.doncal.rxjavademo.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding; //自动生成ActivityMainBinding类,命名规则是布局文件使用驼峰规则来命名
final User mUser = new User(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
mUser.name.set("plbear"); //修改变量
mUser.passwd.set("123456");
binding.setUser(mUser);//设置layout文件中的user值
// binding.setClickHandler(new ClickHandler());//2.设置layout文件中的clickHandler值
binding.button.setOnClickListener(v ->{
mUser.passwd.set("change name too"); //1.可以用这种方式设置点击事件,点击后password被设置为change name too
});
} //2.也可以用这种方式设置点击事件,点击之后,name控件中的值变为 change name
//设置前需要在前面设置binding.setClickHandler
// public class ClickHandler{
// public View.OnClickListener btnClickHandler = v -> {
// mUser.name.set("change name");
// };
// }
}

三、换一种实现

前面利用ObservableFiled来实现,这种实现方式比较适合于细粒度的实现,但是一旦大量的数据都是通过MVVM的方式来做,这种实现显然是不合时宜的。那就再找一种数据类的实现方式:

 package com.plbear.doncal.rxjavademo;

 import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.ObservableField; public class User extends BaseObservable{
@Bindable
private String name; @Bindable
private String passwd; public void setPasswd(String passwd){
this.passwd = passwd;
notifyPropertyChanged(BR.passwd);
} public void setName(String name){
this.name = name;
notifyPropertyChanged(BR.name);
} public String getName(){
return this.name;
} public String getPasswd(){
return this.passwd;
}
}

这里有两个坑需要注意下:


  • Bindable标签,这个标签可以用在变量上面,也可以用在getName和getPasswd上面
  • notifyPropertyChanged的时候,一定要通知到Br.passwd,而不是BR.user

Android MVVM小结的更多相关文章

  1. Android MVVM框架RoboBinding初探

    RoboBinding是一个实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架.MVVM模式是MVC模式的重要更新,使得项目结构更加的优美,易于维护以及方便 ...

  2. 如何构建Android MVVM 应用框架

    概述 说到Android MVVM,相信大家都会想到Google 2015年推出的DataBinding框架.然而两者的概念是不一样的,不能混为一谈.MVVM是一种架构模式,而DataBinding是 ...

  3. android MVVM(1)用LiveData关联VM 与 V

    1.官方文档 MVVM 官方文档: https://developer.android.com/jetpack/docs/guide ViewModel 文档: https://developer.a ...

  4. Android开发利器之Data Binding Compiler V2 —— 搭建Android MVVM完全体的基础

    原创声明: 该文章为原创文章,未经博主同意严禁转载. 前言: Android常用的架构有:MVC.MVP.MVVM,而MVVM是唯一一个官方提供支持组件的架构,我们可以通过Android lifecy ...

  5. android mvvm初探

    目前google的databinding library还处在rc版,其中编译器发挥了主要作用.目前也只是在android studio开发环境中支持. mvvm能够大大降低模块间的耦合度,在开发过程 ...

  6. android基础小结

    (注:此小结文档在全屏模式下观看效果最佳) 2016年3月1日,正式开始了我的android学习之路. 最最开始的,当然是学习怎样搭载环境了,然而苦逼的我在win10各种坑爹的指引下还是安装了一个星期 ...

  7. Android技巧小结之新旧版本Notification

    最近开发用到了通知功能,但有几个地方老是提示deprecated,然后就找了篇文章学习了下新旧版本的不同. Notification即通知,用于在通知栏显示提示信息. 在较新的版本中(API leve ...

  8. android MVVM(2)用数据绑定关联VM 与 V

    1.官方文档 https://developer.android.com/topic/libraries/data-binding/architecture 2.简介 数据绑定库 可与MVVM 架构组 ...

  9. android mvvm

    android studio 需要gradle 1.5.0以上才支持 dependencies { classpath 'com.android.tools.build:gradle:1.5.0'} ...

随机推荐

  1. html5--6-57 阶段练习6-折叠导航栏

    html5--6-57 阶段练习6-折叠导航栏 实例 @charset="UTF-8"; *{ ; ; } h3+div{ ; overflow: hidden; transiti ...

  2. 一步一步学Silverlight 2系列(17):数据与通信之ADO.NET Data Services

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  3. A+B Problem && OJ推荐【持续更新】

    目录 List 前言 长郡 Position: code 1. 2. 持续更新,么么哒 List 前言 有没有觉得写这篇文章很奇怪,这个还是有原因的.①很多OJ都有着道题,所以发个博客②这可以介绍很多 ...

  4. I.MX6 新版u-boot分析

    /******************************************************************* * I.MX6 新版u-boot分析 * 说明: * 因为一些 ...

  5. Luogu网校听课笔记(自用

    TG助学课——noilinux 8.2 深夜 Noi Linux的使用——darkflames的博客 #include<bits/stdc++.h> using namespace std ...

  6. 【前端】CentOS 7 系列教程之四: 配置 git 服务器自动部署

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_4.html 安装pm2守护进程,备用 npm install -g pm2 创建/srv/www文件夹 ...

  7. Linux环境下在Tomcat上部署JavaWeb工程

    本文讲解如何将我们已经编译好的JavaWeb工程在Linux环境下的Tomcat上进行部署,总体上的思路是和Windows下JavaWeb项目部署到tomcat差不多,具体步骤和命令如下. 注:部署之 ...

  8. 浅谈getAttribute兼容性

    最近终于证实tag.setAttribute("style", "color:#000;");在IE7中不起作用.于是百度了一些解决办法. IE的setAttr ...

  9. Hackerearth: Mathison and the Pokémon fights

    Mathison and the Pokémon fights code 这是一道比较有意思,出的也非常好的题目. 给定$n$个平面上的点$(x_i, y_i)$,(允许离线地)维护$Q$个操作:1. ...

  10. spring+mybatis下delete和insert返回值-2147482646

    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"&g ...