Data Binding – Part 5

来源:互联网 发布:mac中文输入法全角 编辑:程序博客网 时间:2024/06/07 12:00


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.

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:

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

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.

This entry was posted in Data Binding, Observable and tagged androiddev on .

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

*

 

 

Powered by WordPress | Design by Nuvio Templates
<iframe name="oauth2relay596121036" id="oauth2relay596121036" src="https://accounts.google.com/o/oauth2/postmessageRelay?parent=https%3A%2F%2Fblog.stylingandroid.com#rpctoken=479104353&amp;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;"></iframe>

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.

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:

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

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.

This entry was posted in Data Binding, Observable and tagged androiddev on .

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

*

 

 

Powered by WordPress | Design by Nuvio Templates
<iframe name="oauth2relay596121036" id="oauth2relay596121036" src="https://accounts.google.com/o/oauth2/postmessageRelay?parent=https%3A%2F%2Fblog.stylingandroid.com#rpctoken=479104353&amp;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;"></iframe>
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 明虾煲 明虾多少钱一斤 明虾养殖 红烧明虾 明虾的价格 青明虾 明虾鸡爪煲 明虾价钱 明虾煲怎么做 椒盐明虾 水煮明虾 明虾怎么清洗 明虾的养殖 苔菜明虾 香辣明虾 白灼明虾 蒜蓉明虾 清蒸明虾 明虾堡 小明虾 明虾鸡爪煲的做法 明虾是什么虾 明虾营养 明虾白菜 爆炒明虾 海明虾 明虾干 水煮明虾的做法 明虾是海鲜吗 明虾的做法大全家常 煎明虾 茄汁明虾的做法 干烧明虾的做法 明虾是河虾还是海虾 干煎明虾 基围虾和明虾的区别 椒盐明虾的做法 基围虾明虾 明虾天妇罗 基围虾和明虾 白灼明虾的做法