1. 需求
假设有一种情况,当我们在EditText里面输入内容的时候,如果此时我们的User已经和EditText关联,那么我们希望当输入框内容改变的时候,User对应的字段也发生变化,反之User发生变化的时候,输入框的内容也会跟着变化。这也是MVVM架构的思想,有了databinding框架,就可以帮我们快速实现一个MVVM架构。
2. 实现
对于我们的Model来说,我们要实现框架提供的BaseObservable,并且用@bindable注解标示要实时变化的字段,实现代码:
package com.example.databinding.databindingdemo.model;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.BindingAdapter;
import android.widget.ImageView;
import com.example.databinding.databindingdemo.BR;
import com.example.databinding.databindingdemo.R;
/**
* Created by user on 2018/3/8.
*/
public class UserInfo extends BaseObservable{
private String name;
@Bindable
private String nickname;
@Bindable
private int resId = R.mipmap.ic_launcher;
private int paLeft = 100;
@Bindable
private boolean vip = true;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
notifyPropertyChanged(BR.nickname);
}
public int getResId() {
return resId;
}
public void setResId(int resId) {
this.resId = resId;
notifyPropertyChanged(BR.resId);
}
public int getPaLeft() {
return paLeft;
}
public void setPaLeft(int paLeft) {
this.paLeft = paLeft;
}
public boolean isVip() {
return vip;
}
public void setVip(boolean vip) {
this.vip = vip;
}
}
XML布局:
双向绑定语法:@={user.nickname}
<?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">
<data>
<variable
name="user"
type="com.example.databinding.databindingdemo.model.UserInfo" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@{user.nickname ?? user.name}" />
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={user.nickname}" />
</LinearLayout>
</layout>
package com.example.databinding.databindingdemo;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import com.example.databinding.databindingdemo.adapter.AppAdapter;
import com.example.databinding.databindingdemo.databinding.ActivityMainBinding;
import com.example.databinding.databindingdemo.handler.MyClick;
import com.example.databinding.databindingdemo.listener.UserClickListener;
import com.example.databinding.databindingdemo.model.UserInfo;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements UserClickListener {
private ActivityMainBinding mainBinding;
private UserInfo userInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
userInfo = new UserInfo();
userInfo.setName("name1");
mainBinding.setUser(userInfo);}
}