Android 的MVVM模式的简单应用
来源:互联网 发布:淘客cms系统 编辑:程序博客网 时间:2024/05/16 19:26
Android 的MVVM模式,大家应该有所听过,我就不说这个MVVM的解释了,我说一下它的特点,那就是数据注入方便,而且能把数据和View绑定起来,十分方便。举个例子:将一个User的类创建对象A并设置数据,然后将这个对象A.name与一个TextView的text属性绑定,然后这个TextView就会显示A.name的数据,然后改变这个A.name的时候,这个TextView显示的数据也会跟着变化
以下是例子代码:
首先在app的build.gradle里添加
dataBinding { enabled true}
其他的代码是
public class User extends BaseObservable{ private String userName; private String realName; private String mobile; private int age; /** * 注意: 在BR里对应的常量为follow */ private boolean isFollow; public User(String realName, String mobile) { this.realName = realName; this.mobile = mobile; Log.d("User", "user construct invoked"); } public User() { } @Bindable public boolean isFollow() { return isFollow; } public void setIsFollow(boolean isFollow) { this.isFollow = isFollow; notifyPropertyChanged(BR.follow); } @Bindable public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; notifyPropertyChanged(BR.userName); } @Bindable public int getAge() { return age; } public void setAge(int age) { this.age = age; notifyPropertyChanged(BR.age); } @Bindable public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; notifyPropertyChanged(BR.realName); } @Bindable public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; notifyPropertyChanged(BR.mobile); }}
public class UpdateUserActivity extends AppCompatActivity { private ActivityUpdateUserBinding binding; private User user; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_update_user); user = new User("Chiclaim", "119"); binding.setUser(user); } //如果(某个)字段发生变化. //1,通过User继承BaseObservable实现 //2,通过ObservableField方式实现 //3,通过Observable Collections的方式 如:ObservableArrayMap //4,当然可以通过binding.setUser(user) [相当于所有的View重新设置一遍] public void updateNameByPOJP(View view) { if ("Johnny".equals(user.getRealName())) { user.setRealName("Chiclaim"); user.setMobile("110"); } else { user.setRealName("Johnny"); user.setMobile("119"); } }}
<?xml version="1.0" encoding="utf-8"?><layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <import type="android.databinding.ObservableMap"/> <variable name="user" type="com.mvvm.model.User"/> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <LinearLayout android:id="@+id/ll_pojo" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="1dp" android:background="#21000000" android:orientation="horizontal" android:padding="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:text="@{user.realName}" android:textSize="14dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:background="@null" android:text="@{user.mobile}" android:textSize="14dp" android:textStyle="bold"/> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="updateNameByPOJP" android:text="修改(By POJO)" android:textSize="14dp"/> </LinearLayout></layout>
再见
阅读全文
0 0
- Android 的MVVM模式的简单应用
- MVVM模式的简单案例
- Android MVC、MVP、MVVM模式思想的应用
- Android之MVVM开发模式和DataBinding的简单用法
- Android的MVVM模式优缺点
- Android MVVM模式的理解
- WPF---MVVM模式简单应用
- MVVM下DataGrid的简单应用
- 关于MVVM模式的简单入门
- Android 基于MVC的MVVM模式开发
- 学习android的MVVM设计模式
- Android开发模式之MVC,MVP和MVVM的简单介绍与区别
- Android开发模式之MVC,MVP和MVVM的简单介绍与区别
- Android开发模式之MVC,MVP和MVVM的简单介绍与区别
- MVVM的简单使用
- MVVM的简单使用
- Silverlight的MVVM模式
- Knockout的MVVM模式
- USACO-Section2.2 preface[贪心]
- leetcode 236. Lowest Common Ancestor of a Binary Tree
- 1428:阵前第一功
- IDEA的使用
- SVN版本库备份和恢复
- Android 的MVVM模式的简单应用
- 时间戳与Date相互转换
- 最近的一些基础笔记
- Redis简介
- FreeRTOS_004_FreeRTOSConfig.h配置文件(一)_通用配置
- 网站系统架构梳理-解决高负载高并发
- mac hdmi 外接显示器没声音
- SQL获取年月日方法
- 兼容ie6 | SWFObject、Flash & json2以及动态JavaScript片段加载