【每天积累一点点】Data Binding Library官方教程翻译

来源:互联网 发布:借贷软件排行 编辑:程序博客网 时间:2024/05/17 05:01

Data Binding Library

This document explains how to use the Data Binding Library to writedeclarative layouts and minimize the glue code necessary to bind yourapplication logic and layouts.

The Data Binding Libraryoffers both flexibility and broad compatibility — it's a support library, soyou can use it with all Android platform versions back to Android 2.1 (API level 7+).

To use data binding,Android Plugin for Gradle 1.5.0-alpha1 or higher is required. See how to update the Android Plugin forGradle.

这篇文章用于说明如何用Data Binding Library去写布局文件并且用尽可能少的胶水代码将组织你的应用逻辑和布局文件组织起来。

Data Binding Library即灵活有具有很好的兼容性-它是一个支持库,可以使用它的最低版本是Android2.1。

Gradle的最低版本要求是1.5.0-alpha1,如需更新请看update the Android Plugin for Gradle.

Build Environment

To get started with Data Binding, download the library fromthe Support repository in the Android SDK manager.

首先从SDK管理器的Support repository中下载这个库。

To configure your app touse data binding, add the dataBinding element to your build.gradle file in the app module.

要使用这个库,你需要在你的app module中的build.gradle中添加dataBinding元素。

Use the following code snippet to configure data binding:

看考下面的代码片段图配置data binding:

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

If you have an appmodule that depends on a library which uses data binding, your app module mustconfigure data binding in its build.gradle file as well.

如果你的应用中的子模块也要用到data binding,这个子模块也要配置build.gradle。

Also, make sure you areusing a compatible version of Android Studio. Android Studio 1.3 and later provides support for databinding as described in Android Studio Support forData Binding.

并且,也要确保你的Studio版本也要兼容databinding。Android Studio 1.3以及更高版本都兼容。

Data Binding Layout Files


Writing your first set of data binding expressions

开始你的第一次data binding表达式

Data-binding layout files are slightly different and startwith a root tag of layout followed by a data elementand a view root element. This view element iswhat your root would be in a non-binding layout file. A sample file looks likethis:

Data-binding布局文件与普通的布局文件有些许的不同,Data-binding布局文件以layout作为根标签,里面包含了data标签和view根元素。这个view根元素就和你以前没有使用Data-binding时的写法一样。下面是一个小栗子。

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

The user variable within data describesa property that may be used within this layout.

Data标签里的user变量是一个布局中可能会用到的属性值。

<variable name="user" type="com.example.User"/>

Expressions within thelayout are written in the attribute properties using the "@{}" syntax. Here, theTextView's text is set to the firstName property of user:

布局文件中控件里的表达式写法是"@{}",例如这里的TextView的text的值就是user对象中的firstName的属性值。

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

Data Object

Let's assume for now that you have a plain-old Java object(POJO) for User:

假如现在有一个简单的java对象User:

public class User {   public final String firstName;   public final String lastName;   public User(String firstName, String lastName) {       this.firstName = firstName;       this.lastName = lastName;   }}

This type of object hasdata that never changes. It is common in applications to have data that is readonce and never changes thereafter. It is also possible to use a JavaBeansobjects:

这种类型的对象永远不会改变。通常在应用程序中之被赋值一次之后不会再发生变化。

或者用一个JavaBean对象。

public class User {   private final String firstName;   private final String lastName;   public User(String firstName, String lastName) {       this.firstName = firstName;       this.lastName = lastName;   }   public String getFirstName() {       return this.firstName;   }   public String getLastName() {       return this.lastName;   }}

From the perspective ofdata binding, these two classes are equivalent. The expression @{user.firstName} used for the TextView's android:textattribute will accessthe firstName field in the former class and the getFirstName() method in the latter class. Alternatively, it will also beresolved tofirstName() if that method exists.

在data binding看来,上面这两个类完全相同。表达式@{user.firstName} 将会访问前一个类的firstName 属性和后一个类的getFirstName()方法。二者选一都可以,事实上,当User的属性(例如firstName)是私有时,@{user.firstName}表达式识别的是方法名,

可以识别getFirstName()firstName(),其它方法无法识别,也不是识别的return值。

Binding Data

By default, a Binding class will be generated based on thename of the layout file, converting it to Pascal case and suffixing"Binding" to it. The above layout file was main_activity.xml so the generate class was MainActivityBinding. This class holds all thebindings from the layout properties (e.g. theuser variable) to the layout's Views and knows how to assignvalues for the binding expressions.The easiest means for creating the bindingsis to do it while inflating:

默认情况下,Binding类会根据布局文件的名字以帕斯卡命名法(驼峰)再加上后缀Binding来自动生成。例如,上面的布局文件的名字是 main_activity.xml 所以自动生成了 MainActivityBinding这个类掌控者布局文件中所有控件绑定着的数据,并且通过表达式来分配值。最简单的创建binding的方式是当填充布局的时候:

@Overrideprotected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);   User user = new User("Test", "User");   binding.setUser(user);}

You're done! Run theapplication and you'll see Test User in the UI. Alternatively, you can get theview via:

上面简单的几行代码就binding成功了,然后运行应用你就会看到“Test”和“User”。

也可以通过下面的方式创建bind。

MainActivityBinding binding = MainActivityBinding.inflate(getLayoutInflater());

If you are using databinding items inside a ListView or RecyclerView adapter, you may prefer to use:

如果在ListView或RecyclerView的adapter中,你也可以通过如下方式创建bind。

ListItemBinding binding = ListItemBinding.inflate(layoutInflater, viewGroup, false);//orListItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false);

Event Handling

更新中。。。。。。

阅读全文
0 0
原创粉丝点击