DataBinding基本功能使用笔记

来源:互联网 发布:linux怎么查看密码密码 编辑:程序博客网 时间:2024/06/06 16:32

DataBinding是一个实现数据和UI绑定的框架,是构建MVVM模式的一个关键工具。
基本用法
1 Gradle启用DataBinding

Android{    ….    databinding{    enabeled true } }

2 创建viewmodel包,包下创建一个UserViewModel,这里实现了双向绑定,需要注意set方法加notifyPropertyChanged(),get方法加@Bindable注解;

import android.databinding.BaseObservable;import android.databinding.Bindable;import com.android.databinding.library.baseAdapters.BR;/** * Created by chentanrong on 2017/3/22. */public class UserViewModel extends BaseObservable{    private boolean gender;//男--true  女--false    private String username;    private String password;    public UserViewModel(boolean gender, String username, String password) {        this.gender = gender;        this.username = username;        this.password = password;    }    @Bindable    public boolean isGender() {        return gender;    }    public void setGender(boolean gender) {        this.gender = gender;        notifyPropertyChanged(BR.gender);    }    @Bindable    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;        notifyPropertyChanged(BR.password);    }    @Bindable    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;        notifyPropertyChanged(BR.password);    }    @Override    public String toString() {        return "UserViewModel{" +                "gender=" + gender +                ", username='" + username + '\'' +                ", password='" + password + '\'' +                '}';    }}

2 在需要绑定的xml中,引用这个ViewMode,其如在activity_main.xml中绑定:

<?xml version="1.0" encoding="utf-8"?><layout>    <data>        <variable            name="userViewModel"            type="com.yulintu.databind.viewmodel.UserViewModel" />    </data>    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:tools="http://schemas.android.com/tools"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        tools:context="com.yulintu.databind.MainActivity">        <EditText            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="@={userViewModel.username}" />//加“=”才能双向绑定        <EditText            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="@={userViewModel.password}" />        <RadioGroup            android:orientation="horizontal"            android:layout_width="match_parent"            android:layout_height="wrap_content">            <RadioButton                android:id="@+id/radioButtonM"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:checked="@={userViewModel.gender}"                android:text="男" />            <RadioButton                android:id="@+id/radioButtonF"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:checked="@={!userViewModel.gender}"                android:text="女" />        </RadioGroup>        <Button            android:id="@+id/buttonRegister"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="注册" />    </LinearLayout></layout>

3 在mainActitiy中,DataBindingh会根据activity_main.xml的名字自动生成一个ActivityMainBinding的类,此时需要将setContentView( R.layout.activity_main)换成ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

public class MainActivity extends AppCompatActivity {    private ActivityMainBinding binding;    private UserViewModel userViewModel;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //setContentView(R.layout.activity_main);        binding= DataBindingUtil.setContentView(this,R.layout.activity_main);        userViewModel =new UserViewModel(true,"chentanrong","123454");        //当viewModel中的值发生改变时的回调        Observable.OnPropertyChangedCallback onPropertyChangedCallback=new Observable.OnPropertyChangedCallback() {            @Override            public void onPropertyChanged(Observable observable, int i) {                if(i==BR.gender&&observable instanceof UserViewModel ){                    Toast.makeText(getApplicationContext(),((UserViewModel) observable).isGender()?"男":"女", Toast.LENGTH_LONG).show();                }                if(i==BR.username&&observable instanceof UserViewModel ){                    Toast.makeText(getApplicationContext(),((UserViewModel) observable).getUsername(), Toast.LENGTH_LONG).show();                }                if(i==BR.password&&observable instanceof UserViewModel ){                    Toast.makeText(getApplicationContext(),((UserViewModel) observable).getPassword(), Toast.LENGTH_LONG).show();                }            }        };        userViewModel.addOnPropertyChangedCallback(onPropertyChangedCallback);        //界面点击事件的回调        binding.buttonRegister.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(getApplicationContext(), userViewModel.toString(), Toast.LENGTH_LONG).show();            }        });        binding.setUserViewModel(userViewModel);    }}
原创粉丝点击