DataBinding使用指南

一、认识DataBinding

DataBinding,2015年IO大会介绍的一个框架,字面理解即为数据绑定,是Google对MVVM在Android上的一种实现,可以直接绑定数据到xml中,并实现自动刷新。

好处:

去掉大部分UI相关代码(比如findViewById、setOnClickListener、setText等)
xml变成UI的唯一真实来源,数据绑定也直接发生在xml
二、DataBinding使用配置

1.环境要求

AndroidStudio1.3以上
gradle插件1.5以上
2.gradle配置

在android中添加:

 dataBinding {
     enabled = true
 }

3.在xml布局文件中添加layout标签

<layout>
    // 原来的layout
</layout>

4.Binding自动生成规则

默认生成规则:xml通过文件名生成,使用下划线分割大小写。
比如activity_demo.xml,则会生成ActivityDemoBinding,item_search_hotel则会生成ItemSearchHotelBinding。也可以自定义生成的class名字,在data中指定class:

<data class=“MyClassName”>
…
</data>

view的生成规则类似,只是由于是类变量,首字母不是大写,比如有一个TextView的id是first_name,则会生成名为firstName的TextView。

使用方法:
1,设置布局文件的值:
Activity中

 Employee employee = new Employee("da 神", "Mark 哈哈");
    ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        binding.tvFirstName.setText(employee.getFirstname());
        binding.tvLastName.setText(employee.getLastname());

2.UI/和事件的绑定
XML布局中

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="employee"
            type="mooc.com.animtest.Employee"/>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:orientation="vertical">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="输入First Name"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="输入 Last Name"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{employee.firstname}"
            android:textSize="22sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{employee.lastname}"
            android:textSize="22sp"/>


    </LinearLayout>
</layout>

Activty中代码:


    Employee employee = new Employee("安卓da 神", " 哈哈 傻子");
    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setEmployee(employee);
    }

事件绑定:
android:onClick
android:onLongClick
android:onTextChanged

public class MainActivity extends AppCompatActivity {


    Employee employee = new Employee("安卓da 神", " 哈哈 傻子");
    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setEmployee(employee);
        binding.setPresenter(new Presenter());
    }

    public class Presenter {

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            employee.setFirstname(s.toString());
            binding.setEmployee(employee);
        }

        public void onClick(View view) {

            Toast.makeText(MainActivity.this, "点到了" + view.getId(), Toast.LENGTH_SHORT).show();
        }

        //监听器绑定
        public void onClickListenerBinding(Employee employee) {
            Toast.makeText(MainActivity.this, employee.getLastname(), Toast.LENGTH_SHORT).show();
        }
    }

}

xml布局文件

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="employee"
            type="mooc.com.animtest.Employee"/>

        <variable
            name="presenter"
            type="mooc.com.animtest.MainActivity.Presenter"/>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:orientation="vertical">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="输入First Name"
            android:onTextChanged="@{presenter.onTextChanged}"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="输入 Last Name"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="@{presenter.onClick}"
            android:text="@{employee.firstname}"
            android:textSize="22sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="@{()->presenter.onClickListenerBinding(employee)}"
            android:text="@{employee.lastname}"
            android:textSize="22sp"/>


    </LinearLayout>
</layout>

基本原理:
android.binding;
BR
XxxBinding

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值