Data Binding – Part 5



id="twttrHubFrameSecure" allowtransparency="true" frameborder="0" scrolling="no" tabindex="0" name="twttrHubFrameSecure" src="https://platform.twitter.com/widgets/hub.e00c464da75b252cb1a028400793cadc.html" style="margin: 0px; padding: 0px; border-width: 0px; border-style: initial; font-size: 14px; vertical-align: baseline; max-width: 100%; color: rgb(68, 68, 68); font-family: 'Open Sans', Helvetica, Arial, sans-serif; line-height: 14px; position: absolute; top: -9999em; width: 10px; height: 10px;">

Styling Android

A technical guide to improving the UI and UX of Android apps

Skip to content

Data Binding – Part 5

At Google I/O 2015 a number of new Android libraries and tools were announced. One of them was the all new Data Binding library and in this series we’ll take a look at the library and explore some of the powerful features it provides.

width="468" height="60" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_0" name="aswift_0" style="margin: 0px 0px 1.71429rem; padding: 0px; border-width: 0px; border-style: initial; vertical-align: baseline; max-width: 100%; left: 0px; position: absolute; top: 0px;">

We’ve covered some pretty useful aspects of Data Binding so far but there is a feature that makes it even more powerful still: Observables.

Observables are really useful when it comes to data which may change over time. To demonstrate this let’s pretend that the Twitter API does not actually return the quoted status within the twitter4j.Status object and we need to perform a separate network call to retrieve it (Twitter doesn’t work like this, but indluge me!). It would not make sense to fetch these up-front, we should only retrieve those that the user wants to retrieve by tapping on the quoted status. We wired the click handler previously – so we would then need to retrieve the quoted status and then update the UI once it was retrieved.

Observables remove the need for us to have to refresh the UI manually when the underlying data changes. As the name suggests an Observable can be observed by another component and that component will receive a callback whenever the Observable changes.

While we can make the entire ModelView class an Observable we can also make individual fileds Observable for use-cases where only a part of the data is likely to change.

Let’s extend our ModelView class to add an additional field plus some methods whihc will allow us to change it:

We already created the field representing the quoted status and we used this previously to enable us to only display the quoted text box when the item has a quoted status, but we didn’t actually expose the quoted status data itself.

Next we’l change our click handler to toggle the contents of the Observable between set and null. If we did actually have to perform a network call here we would initiate it, update the UI to indicate a loading state, and then wait. When the network call completed we’d simply update the contents of the Observable.

Finally we’ll add the additional Views required to display the quoted status. These are pretty much identical to the ones we already have to display the main status – they are just bound to the values within the Observable instead. We also have some visibility bindings to toggle the visibility based on whether the Observable contains data or a null .data.Status object:

That’s it – we don’t need to do anything more. The UI will automatically update whenever theObservable changes:

class="youtube-player" type="text/html" width="480" height="300" src="https://www.youtube.com/embed/LXXyFqJ8owo?version=3&rel=1&fs=1&autohide=2&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent" frameborder="0" allowfullscreen="true" style="margin: 0px 0px 1.71429rem; padding: 0px; border-width: 0px; border-style: initial; vertical-align: baseline; max-width: 100%;">

That completes our basic overview of the new Data Binding library. I must confess that I was initially skeptical when the Data Binding was announced as it immediately reminded me of JSP (and I still have the emotional scars from developing those). It is for this reason that I advocate keeping the actual logic outside of the layout files as much as possible. However having had a play with it for this series I am pleasantly surprised and it seems to do an awful lot right. Once it exits beta, I would certainly consider using it on commercial projects.

The source code for this article is available here.

© 2015, Mark Allison. All rights reserved. This article originally appeared on Styling Android.

Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License

width="468" height="60" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_1" name="aswift_1" style="margin: 0px 0px 1.71429rem; padding: 0px; border-width: 0px; border-style: initial; vertical-align: baseline; max-width: 100%; left: 0px; position: absolute; top: 0px;">
id="twitter-widget-0" scrolling="no" frameborder="0" allowtransparency="true" class="twitter-share-button twitter-share-button-rendered twitter-tweet-button" title="Twitter Tweet Button" src="https://platform.twitter.com/widgets/tweet_button.e00c464da75b252cb1a028400793cadc.en.html#_=1447751701232&count=none&dnt=false&id=twitter-widget-0&lang=en&original_referer=https%3A%2F%2Fblog.stylingandroid.com%2Fdata-binding-part-5%2F%23more-3343&related=AddToAny%2Cmicropat&size=m&text=Data%20Binding%20%E2%80%93%20Part%205&type=share&url=http%3A%2F%2Fblog.stylingandroid.com%2Fdata-binding-part-5%2F" data-url="http://blog.stylingandroid.com/data-binding-part-5/" style="margin: 0px; padding: 0px; border-width: 0px; border-style: initial; max-width: none; display: inline; position: absolute; visibility: hidden; width: 0px; height: 0px; vertical-align: baseline !important;"> Share

CC BY-NC-SA 4.0 
Data Binding – Part 5 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.

2 thoughts on “Data Binding – Part 5

  1. Albert Braun

    Thanks for highlighting and explaining the observable feature of data binding. (It’s yet another aspect of data binding I had no clue about.)

    Also, thanks for offering optimism on the general viability and quality of data binding. I want to believe (but, as we all know too well, sometimes a new Android SDK feature doesn’t quite merit our confidence…)

    Reply 
  2. Amit

    Hello

    Nice tutorial.

    I am also trying to implement data binding concept. I would like to fetch the value form model which contain list of another model and want value from that.
    for e.x.
    Lets say Status model has one property arrayList , image contain Url and image name and i am passing status model to my xml layout. so how can i get value from arraylist of Images.

    Please help me out. I am stuck with this.

    Reply 

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA Image
Refresh Image

*

 

 

name="fb_xdm_frame_https" frameborder="0" allowtransparency="true" allowfullscreen="true" scrolling="no" title="Facebook Cross Domain Communication Frame" aria-hidden="true" tabindex="-1" id="fb_xdm_frame_https" src="https://s-static.ak.facebook.com/connect/xd_arbiter/wjDNIDNrTQG.js?version=41#channel=f188eb0958&origin=https%3A%2F%2Fblog.stylingandroid.com" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; vertical-align: baseline; max-width: 100%;">
name="oauth2relay596121036" id="oauth2relay596121036" src="https://accounts.google.com/o/oauth2/postmessageRelay?parent=https%3A%2F%2Fblog.stylingandroid.com#rpctoken=479104353&forcesecure=1" tabindex="-1" style="margin: 0px; padding: 0px; border-width: 0px; border-style: initial; font-size: 14px; vertical-align: baseline; max-width: 100%; color: rgb(68, 68, 68); font-family: 'Open Sans', Helvetica, Arial, sans-serif; line-height: 14px; width: 1px; height: 1px; position: absolute; top: -100px;">
id="twttrHubFrameSecure" allowtransparency="true" frameborder="0" scrolling="no" tabindex="0" name="twttrHubFrameSecure" src="https://platform.twitter.com/widgets/hub.e00c464da75b252cb1a028400793cadc.html" style="margin: 0px; padding: 0px; border-width: 0px; border-style: initial; font-size: 14px; vertical-align: baseline; max-width: 100%; color: rgb(68, 68, 68); font-family: 'Open Sans', Helvetica, Arial, sans-serif; line-height: 14px; position: absolute; top: -9999em; width: 10px; height: 10px;">

Styling Android

A technical guide to improving the UI and UX of Android apps

Skip to content

Data Binding – Part 5

At Google I/O 2015 a number of new Android libraries and tools were announced. One of them was the all new Data Binding library and in this series we’ll take a look at the library and explore some of the powerful features it provides.

width="468" height="60" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_0" name="aswift_0" style="margin: 0px 0px 1.71429rem; padding: 0px; border-width: 0px; border-style: initial; vertical-align: baseline; max-width: 100%; left: 0px; position: absolute; top: 0px;">

We’ve covered some pretty useful aspects of Data Binding so far but there is a feature that makes it even more powerful still: Observables.

Observables are really useful when it comes to data which may change over time. To demonstrate this let’s pretend that the Twitter API does not actually return the quoted status within the twitter4j.Status object and we need to perform a separate network call to retrieve it (Twitter doesn’t work like this, but indluge me!). It would not make sense to fetch these up-front, we should only retrieve those that the user wants to retrieve by tapping on the quoted status. We wired the click handler previously – so we would then need to retrieve the quoted status and then update the UI once it was retrieved.

Observables remove the need for us to have to refresh the UI manually when the underlying data changes. As the name suggests an Observable can be observed by another component and that component will receive a callback whenever the Observable changes.

While we can make the entire ModelView class an Observable we can also make individual fileds Observable for use-cases where only a part of the data is likely to change.

Let’s extend our ModelView class to add an additional field plus some methods whihc will allow us to change it:

We already created the field representing the quoted status and we used this previously to enable us to only display the quoted text box when the item has a quoted status, but we didn’t actually expose the quoted status data itself.

Next we’l change our click handler to toggle the contents of the Observable between set and null. If we did actually have to perform a network call here we would initiate it, update the UI to indicate a loading state, and then wait. When the network call completed we’d simply update the contents of the Observable.

Finally we’ll add the additional Views required to display the quoted status. These are pretty much identical to the ones we already have to display the main status – they are just bound to the values within the Observable instead. We also have some visibility bindings to toggle the visibility based on whether the Observable contains data or a null .data.Status object:

That’s it – we don’t need to do anything more. The UI will automatically update whenever theObservable changes:

class="youtube-player" type="text/html" width="480" height="300" src="https://www.youtube.com/embed/LXXyFqJ8owo?version=3&rel=1&fs=1&autohide=2&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent" frameborder="0" allowfullscreen="true" style="margin: 0px 0px 1.71429rem; padding: 0px; border-width: 0px; border-style: initial; vertical-align: baseline; max-width: 100%;">

That completes our basic overview of the new Data Binding library. I must confess that I was initially skeptical when the Data Binding was announced as it immediately reminded me of JSP (and I still have the emotional scars from developing those). It is for this reason that I advocate keeping the actual logic outside of the layout files as much as possible. However having had a play with it for this series I am pleasantly surprised and it seems to do an awful lot right. Once it exits beta, I would certainly consider using it on commercial projects.

The source code for this article is available here.

© 2015, Mark Allison. All rights reserved. This article originally appeared on Styling Android.

Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License

width="468" height="60" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_1" name="aswift_1" style="margin: 0px 0px 1.71429rem; padding: 0px; border-width: 0px; border-style: initial; vertical-align: baseline; max-width: 100%; left: 0px; position: absolute; top: 0px;">
id="twitter-widget-0" scrolling="no" frameborder="0" allowtransparency="true" class="twitter-share-button twitter-share-button-rendered twitter-tweet-button" title="Twitter Tweet Button" src="https://platform.twitter.com/widgets/tweet_button.e00c464da75b252cb1a028400793cadc.en.html#_=1447751701232&count=none&dnt=false&id=twitter-widget-0&lang=en&original_referer=https%3A%2F%2Fblog.stylingandroid.com%2Fdata-binding-part-5%2F%23more-3343&related=AddToAny%2Cmicropat&size=m&text=Data%20Binding%20%E2%80%93%20Part%205&type=share&url=http%3A%2F%2Fblog.stylingandroid.com%2Fdata-binding-part-5%2F" data-url="http://blog.stylingandroid.com/data-binding-part-5/" style="margin: 0px; padding: 0px; border-width: 0px; border-style: initial; max-width: none; display: inline; position: absolute; visibility: hidden; width: 0px; height: 0px; vertical-align: baseline !important;"> Share

CC BY-NC-SA 4.0 
Data Binding – Part 5 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.

2 thoughts on “Data Binding – Part 5

  1. Albert Braun

    Thanks for highlighting and explaining the observable feature of data binding. (It’s yet another aspect of data binding I had no clue about.)

    Also, thanks for offering optimism on the general viability and quality of data binding. I want to believe (but, as we all know too well, sometimes a new Android SDK feature doesn’t quite merit our confidence…)

    Reply 
  2. Amit

    Hello

    Nice tutorial.

    I am also trying to implement data binding concept. I would like to fetch the value form model which contain list of another model and want value from that.
    for e.x.
    Lets say Status model has one property arrayList , image contain Url and image name and i am passing status model to my xml layout. so how can i get value from arraylist of Images.

    Please help me out. I am stuck with this.

    Reply 

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA Image
Refresh Image

*

 

 

name="fb_xdm_frame_https" frameborder="0" allowtransparency="true" allowfullscreen="true" scrolling="no" title="Facebook Cross Domain Communication Frame" aria-hidden="true" tabindex="-1" id="fb_xdm_frame_https" src="https://s-static.ak.facebook.com/connect/xd_arbiter/wjDNIDNrTQG.js?version=41#channel=f188eb0958&origin=https%3A%2F%2Fblog.stylingandroid.com" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; vertical-align: baseline; max-width: 100%;">
name="oauth2relay596121036" id="oauth2relay596121036" src="https://accounts.google.com/o/oauth2/postmessageRelay?parent=https%3A%2F%2Fblog.stylingandroid.com#rpctoken=479104353&forcesecure=1" tabindex="-1" style="margin: 0px; padding: 0px; border-width: 0px; border-style: initial; font-size: 14px; vertical-align: baseline; max-width: 100%; color: rgb(68, 68, 68); font-family: 'Open Sans', Helvetica, Arial, sans-serif; line-height: 14px; width: 1px; height: 1px; position: absolute; top: -100px;">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值