Skip to content

mmorenodev/AndroidFundamentals-DataBinding

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 

Repository files navigation

AndroidFundamentals-DataBinding

Learning about Data Binding on Pluralsight.

Data Binding Library Process

  1. Create a binding from a layout
  2. Retrieve the data
  3. Bind the data to the view

This is possible because of code generation.

Data Binding Compilaiton Process

image

Terms

Layout - XML file descriptioin View - Java file subclassing an activity, fragment, or view Binding class - A Java file generated using code generation Code Generation - Compile time process to create code Gradle Plugin - Assists in custom compilation

We need to use an Android Studio version that SUPPORTS THE USE OF DATA BINDING LIBRARY.

image

Simple Data Binding

Prepariing an XML to use DataBinding

  1. Surround all the view hierarchy defined in XML with the tag
  2. Inside this new XML you can have a section called where you declare your variables

image

Member Access (Data Binding Expressions)

It will find the first item that matches the following pattern:

  1. A public method with a named prefixed by get
  2. A public method with a name
  3. A public field with a name

image

If there's not a matching public member you will receive an data binding error on compilation. The error message will indicate it cannot find an accessor.

image

View Binding Strategies

  • Inflate the view, then bind it.

image

  • Inflate and bind at the same time

image

image

  • Retrieve binding from an already-inflated view One last note here, if you created a binding previously, but don't have a reference to it you can get that reference using the DataBindingUtil.

image

Binding includes

image

In the separated layout we declare the same variable and same name image

Binding Custom Views

Binding a custom view requires a slightly different tag than binding an activity or fragment and also requires a little more work than a simple include.

image

image

image

In this case, our view will be inflated along with the activity, so we need to choose a binding method that takes an inflated view as a parameter. We can use either DataBindingUtil or DataViewbinding.

image

onAttachToWindow() method is called before 'onDraw()' which makes sense because you need the data before actually drawing the view.

image

image

image

image

Layout Expression Language - The power inside the "@{}"

The binding library helps you control an element's value and state.

image

The expression language is not designed for object creation, so you'll not be able to use the new keyword. If you've derived a class as variable, you will not be able to use the super keyword either to access base class members.

image

image

We can reference string resources inside EXPRESSION LANGUAGE

image

About

Learning about Data Binding on Pluralsight.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors