Data Binding – Part 5

转载 2015年11月17日 17:34:11


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.

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

*

 

 

<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.

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

*

 

 

<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>
阅读全文

由Flex 3進入Flex 4的必備知識 PART 5 – Flex 4的雙向Data Binding

在Flex4中提供了一個比較有趣的功能,他可以幫助使用者進行雙向的DataBinding這個動作,在原廠的文件中他稱之為 Two Way Data Binding,從字面上應該不難理解他的功用,舉個例...

#448 – 在Grid中显示数据绑定元素的集合(Data Binding Elements in a Collection to a Grid)

假设你有一个集合,想将集合的数据显示在Grid上,数据所显示行和列根据集合的内容动态设置。你需要在集合的单个实例中有指定行和列的属性并对其进行绑定。 你可以使用Grid 面板作为ItemsContro...

Windows Phone Toolkit MultiselectList in depth| Part2: Data Binding

原文地址:http://windowsphonegeek.com/articles/Windows-Phone-Toolkit-MultiselectList-in-depth-Part2-Data-...

Caliburn Micro Part 2: Data Binding and Events

In my previous blog post I showed you how to get started with using the Caliburn Micro Framework i...

#395 – 通过数据绑定丰富ListBox 的内容(Rich ListBox Content using Data Binding)

在上一篇讲述SnapsToDevicePixels 属性的文章中,我们使用了一个ListBox 的例子列出了很多电影。里面每一个条目都有包括缩略图在内的若干个数据项组成。 下面我们看一下如何在WPF...

5.NativeScript Data Binding

Data Binding Overview Data binding is the process ofconnecting application user interface (UI) to ...

Network Virtualization: Beyond VLANs – Part 5: Virtual Routing and Forwarding (VRF)

Part 1: VLANs Part 2: Subinterfaces Part 3: Switch Virtual Interfaces (SVIs) Part 4: Tunnels Par...

[翻译Building the Game: Part 5 – Static Level Geometry

还不错的文章,今天木有翻译完,原因是没心情了,明天翻译完,谢谢 Building the Game: Part 5 - Static Level Geometry See the code...
  • hack2me
  • hack2me
  • 2011年12月13日 20:23
  • 835

使用internal(com.android.internal)和hidden(@hide)APIs – Part 5

原文路径:https://devmaze.wordpress.com/2011/01/19/using-com-android-internal-part-5-summary-and-example/...

Effects with the Pixel Bender Toolkit – Part 5: Applying a filter to an image in Flash

Requirements User level BeginningRequired products Flash Player 10 and later Flash Professional (Dow...