Android DataBinding detailed

Source: Internet
Author: User

Official Introduction Address:http://developer.android.com/intl/zh-cn/tools/data-binding/guide.html

The Data Binding Library brought by the Google IO conference makes it easy for Android developers to implement the MVVM schema pattern. Using databinding can improve application development and make your code cleaner and more elegant. What is the MVVM mode is actually a layer of viewmodel between the view and the model layer, avoiding the view layer directly manipulating the model layer in the previous MVC pattern, thus making the code structure clearer. Interested can take a look at the previous reprint of an introduction about the development model: http://blog.csdn.net/xiangzhihong8/article/details/52671151

Today is to introduce the databining (data binding), for the advantages and disadvantages of this I do not say, there are pros and cons: Click to open the link.

Configuring the Environment

The default requires Android studio to be upgraded to 1.3 (most of which is now satisfied), because DataBinding's build.gradle needs to meet the minimum 1.3 (Android Studio has built-in Android Data Support of the Binding framework ). Just add the following script to the Build.gradle when you use it:

dataBinding {        enabled = True    }

external libraries:

adapters-1.1    defines some of the databinding components baseLibrary-2.1.3    defines some databinding annotation and callback interfaces compiler-2.1.3    Defines some of the tool classes used to compile databinding library-1.1     defines some observable basic types
The DataBinding Library has changed the way layout files are written in the traditional Android development process, and by ViewModel, the view and model are bound together, you only need to modify the value of the model layer, and the corresponding view layer will listen to automatically modify itself. (In fact, the separation of the page and the data is achieved)


With so many theories, how do you use databinding?

First we need to write a layout, but the data Binding layout is not the same as the traditional layout, the starting root tag is layout, the next Data element and the root element of a view. This view element is the root element of a layout file that you do not use with Data binding.

Generally in the order of formal writing code we will first define a ViewModel class, as follows:

public class Usermodel {    private  String firstName;    Private  String lastName;    Public Usermodel (String firstName, String lastName) {        this.firstname = firstName;        This.lastname = LastName;    }    Public String Getfirstname () {        return this.firstname;    }    Public String Getlastname () {        return this.lastname;    }}

and then in implementing a layout (the trick is right here):

<?xml version= "1.0" encoding= "Utf-8"? ><layout xmlns:android= "http://schemas.android.com/apk/res/ Android "> <data> <variable name=" user "type= "Com.xzh.databinding.model.UserModel"/></data> <linearlayout android:layout_width= "match_parent" android:layout_height= "match_p            Arent "android:orientation=" vertical "> <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_cont Ent "android:text="@{user.lastname}"/> </LinearLayout></layout>
Note that this layout file name, databinding will generate a xxbinding class based on the file name of the layout, this class inherits from Viewdatabiding; If the layout file name is Content_main.xml , a contentmainbinding class is generated, which, according to the official explanation, is automatically stripped of the layout file name, followed by a camel-like naming convention, followed by a binding suffix.

Com.xzh.databinding Generate A class that inherits from Viewdatabinding based on the name of the XML file. For example, here the XML file is called Activity_main.xml, then the generated class is activitymainbinding.

Finally, it is necessary to implement the data binding of view and model through ViewModel (often written in the activity layer, if the project is larger, it is recommended to separate the network request).

private void Getsearchdata (String search) {        binding.progressBar.setVisibility (view.visible);        Moviehttpmanager.searchmovies (Search, new moviehttpmanager.imovieresponse<list<movie>> () {            @ Override public            void OnData (list<movie> List) {                Movieadapter madapter = new Movieadapter ( Movieactivity.this, list);                Binding.recyclerView.setAdapter (madapter);                Binding.progressBar.setVisibility (View.gone);}}        );    }
The Adapter used here is recyclerview.adapter. However, although this kind of writing is relatively new, but it is not very easy for beginners to accept. Introduction to the principle Please link: databinding principle of the big secret

Source: Click to open link







Android DataBinding detailed

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.