美文网首页DataBinding
Android中Data Binding学习使用

Android中Data Binding学习使用

作者: 紫苓 | 来源:发表于2016-10-16 11:01 被阅读409次

    1、官方文档

    2、基本操作

    关于DataBinding具体有什么优势和坑,网上资料太多了,这里只学习下怎么使用。
    首先一个Model是必须的

    public class UserInfo {
        private String name;
        private String age;
        private String provice;
        private String country;
    
        public UserInfo(String name, String age, String provice, String country) {
            this.name = name;
            this.age = age;
            this.provice = provice;
            this.country = country;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAge() {
            return age;
        }
    
        public void setAge(String age) {
            this.age = age;
        }
    
        public String getProvice() {
            return provice;
        }
    
        public void setProvice(String provice) {
            this.provice = provice;
        }
    
        public String getCountry() {
            return country;
        }
    
        public void setCountry(String country) {
            this.country = country;
        }
    
        public String getAddress() {
            return provice + country;
        }
    }
    

    Model和xml绑定

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android">
    
        <data>
            <variable
                name="user"
                type="com.study.databinding.model.UserInfo" />
        </data>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{user.name}" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{user.address}" />
        </LinearLayout>
    </layout>
    

    和具体对象绑定

    public class MainFragment extends Fragment{
    
        public static MainFragment newInstance(){
            MainFragment fragment = new MainFragment();
            return fragment;
        }
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            FragmentMainBinding binding = DataBindingUtil.inflate(inflater,R.layout.fragment_main,container,false);
            View rootView = binding.getRoot();
    
            UserInfo userInfo = new UserInfo("张三","45","江苏省苏州市","苏州工业园区");
            binding.setUser(userInfo);
    
            return rootView;
        }
    }
    

    脚本配置

    android {
        ....
        dataBinding {
            enabled = true
        }
    }
    

    3、两种对事件处理的方式

    3.1 Method References方式:把相关View事件交给指定的方法来完成响应。
    <data>
            <variable
                name="user"
                type="com.study.databinding.model.UserInfo" />
    
            <variable
                name="eventHandler"
                type="com.study.databinding.MainFragment" />
        </data>
    
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            FragmentMainBinding binding = DataBindingUtil.inflate(inflater,R.layout.fragment_main,container,false);
            View rootView = binding.getRoot();
    
            UserInfo userInfo = new UserInfo("张三","45","江苏省苏州市","苏州工业园区");
            binding.setUser(userInfo);
            binding.setEventHandler(this);
            return rootView;
        }
    
    public void onNameClick(View view){
            Log.v("wwwwwwwwwweeeeeeeeeeeee","Name is Clicked");
        }
    
        public void onAddressClick(View view){
            Log.v("wwwwwwwwwweeeeeeeeeeeee","Address is Clicked");
        }
    
    3.2 Listener Bindings

    这种事件绑定和处理方式基本上和Method Reference类似,只不过它比Method Reference好的地方在于,事件处理者能够接收任意的数据。这种事件处理方式要求gradle版本为2.0及以上。

    public class EventParams {
        private String name;
        private String address;
    
        public EventParams(String name, String address) {
            this.name = name;
            this.address = address;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        @Override
        public String toString() {
            return "EventParams{" +
                    "name='" + name + '\'' +
                    ", address='" + address + '\'' +
                    '}';
        }
    }
    
    <variable
                name="eventParams"
                type="com.study.databinding.model.EventParams" />
    
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            FragmentMainBinding binding = DataBindingUtil.inflate(inflater,R.layout.fragment_main,container,false);
            View rootView = binding.getRoot();
    
            UserInfo userInfo = new UserInfo("张三","45","江苏省苏州市","苏州工业园区");
            binding.setUser(userInfo);
            binding.setEventHandler(this);
            binding.setEventParams(new EventParams("张三","qqqqqq"));
            return rootView;
        }
    
    public void onSaveAddress(EventParams params){
            Log.v("wwwwwwwwwweeeeeeeeeeeee",""+ params.toString());
        }
    

    4、对象刷新

    这个应该是DataBinding的一大亮点了,在对象里面数据发生改变的时候可以自动刷新UI。数据刷新有三种方式:

    4.1 Observable Objects 对整个对象都进行监听刷新
    package com.study.databinding.model;
    
    import android.databinding.BaseObservable;
    import android.databinding.Bindable;
    
    import com.study.databinding.BR;
    
    
    /**
     * Created by luozheng on 2016/10/13.
     */
    
    public class UserInfo extends BaseObservable{
        private String name;
        private String age;
        private String provice;
        private String country;
        private String email;
    
        public UserInfo(String name, String age, String provice, String country) {
            this.name = name;
            this.age = age;
            this.provice = provice;
            this.country = country;
        }
    
        @Bindable
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
            notifyPropertyChanged(BR.email);
        }
    
        @Bindable
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
            notifyPropertyChanged(BR.name);
        }
    
        @Bindable
        public String getAge() {
            return age;
        }
    
        public void setAge(String age) {
            this.age = age;
            notifyPropertyChanged(BR.age);
        }
    
        public String getProvice() {
            return provice;
        }
    
        public void setProvice(String provice) {
            this.provice = provice;
        }
    
        public String getCountry() {
            return country;
        }
    
        public void setCountry(String country) {
            this.country = country;
        }
    
        @Bindable
        public String getAddress() {
            return provice + country;
        }
        public void setAddress(String address) {
            notifyPropertyChanged(BR.address);
        }
    
        @Override
        public String toString() {
            return "UserInfo{" +
                    "name='" + name + '\'' +
                    ", age='" + age + '\'' +
                    ", provice='" + provice + '\'' +
                    ", country='" + country + '\'' +
                    ", email='" + email + '\'' +
                    '}';
        }
    }
    
    
    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android">
    
        <data>
            <variable
                name="user"
                type="com.study.databinding.model.UserInfo" />
    
            <variable
                name="eventHandler"
                type="com.study.databinding.MainFragment" />
    
            <variable
                name="eventParams"
                type="com.study.databinding.model.UserInfo" />
        </data>
    
        <LinearLayout
            android:layout_margin="30dip"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <TextView
                android:textSize="30sp"
                android:layout_margin="20dip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="@{eventHandler::onNameClick}"
                android:text="@{user.name}" />
    
            <TextView
                android:textSize="30sp"
                android:layout_margin="20dip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="@{() -> eventHandler.onSaveAddress(eventParams)}"
                android:text="@{user.address}" />
    
            <TextView
                android:textSize="30sp"
                android:layout_margin="20dip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="邮箱地址信息"
                android:text="@{user.email}" />
    
            <EditText
                android:id="@+id/editEmail"
                android:textSize="30sp"
                android:layout_margin="20dip"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
            <Button
                android:textSize="30sp"
                android:layout_margin="20dip"
                android:text="数据刷新"
                android:onClick="@{eventHandler::onRefresh}"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </layout>
    
    
    public class MainFragment extends Fragment{
    
        FragmentMainBinding dataBinding;
        UserInfo userInfo;
        EditText editEmail;
    
    
        public static MainFragment newInstance(){
            MainFragment fragment = new MainFragment();
            return fragment;
        }
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            dataBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_main,container,false);
            View rootView = dataBinding.getRoot();
            editEmail = (EditText) rootView.findViewById(R.id.editEmail);
    
            userInfo = new UserInfo("张三","45","江苏省苏州市","苏州工业园区");
            dataBinding.setUser(userInfo);
            dataBinding.setEventHandler(this);
            dataBinding.setEventParams(userInfo);
            return rootView;
        }
    
        public void onNameClick(View view){
            Log.v("wwwwwwwwwweeeeeeeeeeeee","Name is Clicked");
        }
    
        public void onAddressClick(View view){
            Log.v("wwwwwwwwwweeeeeeeeeeeee","Address is Clicked");
        }
    
        public void onSaveAddress(UserInfo params){
            Log.v("wwwwwwwwwweeeeeeeeeeeee",""+ params.toString());
        }
    
        public void onRefresh(View view){
            userInfo.setName("张君宝");
            userInfo.setEmail(editEmail.getText().toString());
        }
    }
    
    4.2 ObservableFields 只对特定的属性进行监听刷新(略)
    4.3 Observable Collections 集合类数据类型刷新(略)

    5、控件with id后可以直接从bingding里面获取到了,不用通过findviewbyid来获取了,也不需要借助第三方注解框架了。

    6、数据双向绑定

    数据双向绑定参考一
    数据双向绑定参考二
    InverseBindingMethod

    相关文章

      网友评论

        本文标题:Android中Data Binding学习使用

        本文链接:https://www.haomeiwen.com/subject/oomwyttx.html