Data binding
- 2. Who we are?
Leonid Olevsky
leonid.olevsky@gmail.com
Yonatan Levin
levinyon@gmail.com
Android
Academy
- 5. What is Data Binding?
Model View ViewModel (MVVM)
Ongoing link between an element in the user interface
and a value
When a variable’s value is updated, the display on the
user’s screen changes automatically
- 16. Expression Language
Mathematical + - / * %
String concatenation +
Logical && ||
Binary & | ^
Unary + - ! ~
Shift >> >>> <<
Comparison == > < >= <=
instanceof
Grouping ()
Literals - character, String,
numeric, null
Cast
Method calls
Field access
Array access []
Ternary operator ?:
- 18. Observable
Observable Objects
listen for changes of all properties on that object
ObservableFields
listen for changes of a field of that object
Observable Collections
listen for changes on dynamic structures to hold data
- 26. Downside
- Define stuff in XML
- Not debuggable
- XML not unit tested
- Run-time errors instead compile time
- Bad IDE support
- 29. MVP vs DataBinding
Presenter react on what happen in the view,
retrieves data, formats and display back in the
view
Data Binding will take over the main
responsibilities of the presenter (react and
display). Rest - will go to the enhanced Model -
ViewModel
- 43. So what we got?
Clean Debuggable code
Decoupled layer
Fully testable UI logic
Fun :)
Editor's Notes
- The presenter acts upon the model and the view. It retrieves data from repositories (the model), and formats it for display in the view.
The thing is that Data Binding framework will take over the main responsibilities of the Presenter (“acting upon the model and the view”), while the remainder is left to the enhanced Model – the ViewModel (“retreiving data from repositories and formatting”).
- The ViewModel is a standard Java class whose sole responsibility is to represent the data behind a single View. It can merge data from multiple sources (Models) and prepare that data for presentation.
- All the binding and updating of data to the view is done through the Data Binding Framework. The ObservableField class allow the View to react on changes to the model, and the XML references to fields allow the framework to push changes back to the ViewModel when the user acts upon the View. You can also programmatically subscribe to changes in fields.
One great advantage of representing the visual state of the View in a standard Java class like this is clear: You can easily unit test the visual behaviour.