DataBinding的初次使用

前言

之前有用过xutil的注解式绑定VIew,但还是觉得不够方便,因为在实际意义上还是做了View的绑定事件,而Databinding则完全不需要做这样的操作,只需要将xml稍稍改造下,并在build.gradle中加上几行代码就可以使用了

###怎么使用 DataBinding

使用方法很简单,Gradle 1.5 alpha 及以上自带支持 DataBinding,仅在app模块的build.gradle中加上几行代码就可以使用了:

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

Binding的命名规则

默认生成规则:xml通过文件名生成,使用下划线分割大小写。 比如activity_demo.xml,则会生成ActivityDemoBinding.java,item_search_hotel则会生成ItemSearchHotelBinding.java。

view的生成规则类似,只是由于是类变量,首字母不是大写,比如有一个TextView的id是first_name,则会生成名为firstName的TextView。 我们也可以自定义生成的class名字,只需要:

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

xml布局最外层添加Layout标签就行了

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

//原来的layout 代码

</layout>

变量绑定

使用data标签,我们就可以在xml中申明变量,在其中使用该变量的field,并通过binding实例set进来。

如:

<data>
    <variable
        name="employee"
        type="com.github.markzhai.databindingsample.Employee"/>
</data>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".DemoActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{employee.lastName}"
        android:layout_marginLeft="5dp"/>

</LinearLayout>

也可以这样

<import type="com.github.markzhai.databindingsample.Employee"
 <variable
        name="employee"
        type="Employee"/>

可以直接使用类名Employee在type,如果有两个类名完全一致的类可以设置alias=""属性设置别名:

<import type="com.github.markzhai.databindingsample.Employee"
<import type="com.github.example.databindingsample.Employee" alias="Employee1"
 <variable
        name="employee"
        type="Employee"/>
 <variable
        name="employee1"
        type="Employee1"/>

然后我们就可以在java代码中使用

binding.setEmployee(employee);
// 或者直接通过setVariable
binding.setVariable(BR.employee, employee);

事件绑定

严格意义上来说,事件绑定也是一种变量绑定。我们可以在xml中直接绑定

android:onClick android:onLongClick android:onTextChanged …

方法引用

通常会在java代码中定义一个名为Handler或者Presenter的类,然后set进来,方法签名需和对应listener方法一致。

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:bind="http://schemas.android.com/apk/res-auto">

    <data>

        <import type="android.view.View"/>

        <variable
            name="employee"
            type="com.github.markzhai.databindingsample.Employee"/>

        <variable
            name="presenter"
            type="com.github.markzhai.databindingsample.DemoActivity.Presenter"/>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        tools:context=".DemoActivity">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="@{presenter.onClick}"
            android:text="@{employee.firstName}"/>

    </LinearLayout>
</layout>

java代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    binding.setPresenter(new Presenter());
    ...
}

public class Presenter {
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        employee.setFirstName(s.toString());
        employee.setFired(!employee.isFired.get());
    }

    public void onClick(View view) {
        Toast.makeText(DemoActivity.this, "点到了", Toast.LENGTH_SHORT).show();
    }
}

转载于:https://my.oschina.net/u/3491516/blog/903910

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值