Intelligence Podcast Secrets Android 6.0 Data Binding Guide

Source: Internet
Author: User

TheData Binding solves a pain point in Android UI Programming, and the official natively supports MVVM The model allows us to use these new features very easily without changing the existing code framework. Before that, some third-party frameworks (Roboandroid) could support the MVVM model, but the framework was too intrusive to be popular.

1. Prepare:

Android Studio Update to 1.3 or higher

2. Note:

The Data Binding is a support Package

3. Create a new Project:

U modify Project 's build.gradleto add a dependencyto the Build script .

Classpath "Com.android.databinding:databinder:1.0-rc1"

You add a plugin to a module that uses the Data Binding and modify the corresponding build.gradle

Applyplugin: ' com.android.databinding '

Once the project has been created, we use a simple example to illustrate the basic usage of Data Binding.

    1. Layout file

With Data Binding , The layout file for XML no longer simply shows UI elements. Therefore, its root node is no longer a viewgroup, but instead becomes layout, and a new node data , as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<layout xmlns:android= "Http://schemas.android.com/apk/res/android" >

<data>
<import type= "Com.example.siyan.sample.Person"/>
<variable
Name= "User"
Type= "Person"/>
</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}"
Android:textsize= "20sp"/>
</LinearLayout>

InLayoutinDatanode, declare aVariableachild Nodes,then you canLayoutused in the. Typeproperty can beJavaBean,POJO (is actuallyJavaclass), note:Typeshould be the full path. nameproperty is an alias for the reference to this class. as follows:

<data>
<variable
Name= "User"
Type= "Com.example.siyan.sample.Person"/>
</data>

Of course, you can also use one of the following notation. As shown below:

<data>
<import type= "Com.example.siyan.sample.Person"/>
<variable
Name= "User"
Type= "Person"/>
</data>

The layout uses the expression "@{}" to set the valueof object . As shown below:

<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@{user.name}"/>

Android:textsize= "20sp"/>

public class Person {
Public final String name;

Public person (String name) {
THIS.name = name;
}

Public String GetName () {
return name;
}
}

    1. Binding Data

Replace Setcontentview ()with Databindingutil.setcontentview ( ), and then create a user object, by binding.setuser (user) with the variable The binding is as follows:

protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Note that amainbinding is generated based on the layout name.
Amainbinding dataBinding = Databindingutil.setcontentview (this, r.layout.a_main);
Person p = new person ("Preach Intelligence Podcast");
Databinding.setuser (P);
}

The Amainbinding class is automatically generated, and all set methods are generated based on the Name property of the variable node .

At this point, a simple data binding is complete. Run the above program, effect:

Intelligence Podcast Secrets Android 6.0 Data Binding Guide

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.