Use details of DataBinding, details of databinding

Source: Internet
Author: User

Use details of DataBinding, details of databinding
Preface:

The first contact with DataBinding was when I first started the company. The original project of the company was the DataBinding. At that time, I checked some materials to get started with the project as soon as possible. After more than a year of use, I plan to summarize and record it.

What is DataBinding?

DataBinding is a data binding framework introduced at the Google I/O conference in 2015. Is an implementation of MVVM on Android by Google. That is to say, DataBinding is a framework for implementing data and UI binding Based on MVVM ideas. With DataBinding, MVVM can also be easily implemented in Android.

Google introduced the framework in Android Studio v1.3.0 released in July 2015 and officially supported it in Android Studio v2.0.0 in April 2016. Two-way binding is not supported at the beginning of the Introduction. After Android Studio 2.1 Preview 3, the official website began to supportBidirectional binding.

Benefits of DataBinding

The most intuitive advantage is that DataBinding can help us reduce a lot of unnecessary code and greatly improve our development efficiency. For example, the chances of using code such as findViewById (), setText (), setVisibility (), and setEnabled () are greatly reduced.

DataBinding mainly solves two problems:
You need to use findViewById and other non-nutritional code multiple times, compromising application performance and annoying
To update the UI data, you need to switch to the UI thread to map the data into various views.

How to Use DataBinding in a project

Add the following code to the build. gradle file in the app directory of Android studio to use DataBinding:

android {    ....    dataBinding {        enabled = true    }}
Basic use of DataBinding

Since data is bound to the UI, the layout file in the DataBinding framework is no longer a simple UI display, but also a data part. Therefore, the layout of the root node is no longerViewGroupInsteadLayout. The data node is added, and the following code is displayed:

     
          
       
        
       
  
 

The above data nodes provide a bridge between data and the UI. A varaiavle is declared in data. This variable provides data for the UI element. type is the type of this variable, and then the data is bound to this variable in Java code. Data is bound to the UI.

After the data is bound, you can use@{......}Use the data as follows:

 
 

The data displayed in TextView is the data bound to the input variable (variable) in the Java code. The code bound to Java is as follows:

ActivityMainBinding binding = DataBindingUtil. setContentView (this, R. layout. activity_main); binding. setInput ("whatever you want ");

ActivityMainBinding is an automatically generated class. Android studio automatically generates a default Binding Class Based on the layout file. The class name is generated based on the layout file name and ends with the suffix "Binding. For example, the Binding class generated by activity_main.xml is ActivityMainBinding.

Through the above, we have completed a simple data binding.

Data nodes in the layout are also supported.Import, So:

    
  

It is equivalent:

    
      
   
  

Classes in the java. lang. * package will be automatically imported and can be directly used. Therefore, the input variable with the type as String above can be used in the layout.

Talk about Data Binding
Understanding the concept of data binding is helpful for DataBinding.

Data Binding simply means to bind the data in the Code to the xml (UI) through a mechanism. Both parties can operate on the data and when the data changes, automatically refresh data.

We tried to use datBinding to create a small example of data binding. In factOne-way bindingAndBidirectional binding.

In one-way binding, the data flow direction is unilateral and can only flow from the code to the UI; the two-way binding data flow direction is bidirectional, when the data in the Business Code changes, the data on the UI can be refreshed. When the user edits the data through UI interaction, the data changes can also be automatically updated to the data in the Business Code.

DataBinding now supports data binding. Two-way binding has little difference with the previous one-way binding, that is, "@ {}" is changed to "@ = {}". For example, the following code can complete text input in EditText and display the input content in TextView in real time.

            
      
     
         
          
     
   
  
 

 

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.