| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1 | page.title=Data Binding Guide |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 2 | page.tags="databinding", "layouts" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 3 | @jd:body |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 4 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 5 | <div id="qv-wrapper"> |
| 6 | <div id="qv"> |
| 7 | <h2> |
| 8 | In this document: |
| 9 | </h2> |
| 10 | |
| 11 | <ol> |
| 12 | <li> |
| 13 | <a href="#build_environment">Build Environment</a> |
| 14 | </li> |
| 15 | |
| 16 | <li> |
| 17 | <a href="#data_binding_layout_files">Data Binding Layout Files</a> |
| 18 | <ol> |
| 19 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 20 | <a href="#writing_expressions">Writing your first data binding |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 21 | expressions</a> |
| 22 | </li> |
| 23 | |
| 24 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 25 | <a href="#data_object">Data Object</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 26 | </li> |
| 27 | |
| 28 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 29 | <a href="#binding_data">Binding Data</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 30 | </li> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 31 | |
| 32 | <li> |
| 33 | <a href="#binding_events">Binding Events</a> |
| 34 | </li> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 35 | </ol> |
| 36 | </li> |
| 37 | |
| 38 | <li> |
| 39 | <a href="#layout_details">Layout Details</a> |
| 40 | <ol> |
| 41 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 42 | <a href="#imports">Imports</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 43 | </li> |
| 44 | |
| 45 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 46 | <a href="#variables">Variables</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 47 | </li> |
| 48 | |
| 49 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 50 | <a href="#custom_binding_class_names">Custom Binding Class Names</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 51 | </li> |
| 52 | |
| 53 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 54 | <a href="#includes">Includes</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 55 | </li> |
| 56 | |
| 57 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 58 | <a href="#expression_language">Expression Language</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 59 | </li> |
| 60 | </ol> |
| 61 | </li> |
| 62 | |
| 63 | <li> |
| 64 | <a href="#data_objects">Data Objects</a> |
| 65 | <ol> |
| 66 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 67 | <a href="#observable_objects">Observable Objects</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 68 | </li> |
| 69 | |
| 70 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 71 | <a href="#observablefields">ObservableFields</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 72 | </li> |
| 73 | |
| 74 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 75 | <a href="#observable_collections">Observable Collections</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 76 | </li> |
| 77 | </ol> |
| 78 | </li> |
| 79 | |
| 80 | <li> |
| 81 | <a href="#generated_binding">Generated Binding</a> |
| 82 | <ol> |
| 83 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 84 | <a href="#creating">Creating</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 85 | </li> |
| 86 | |
| 87 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 88 | <a href="#views_with_ids">Views With IDs</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 89 | </li> |
| 90 | |
| 91 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 92 | <a href="#variables">Variables</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 93 | </li> |
| 94 | |
| 95 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 96 | <a href="#viewstubs">ViewStubs</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 97 | </li> |
| 98 | |
| 99 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 100 | <a href="#advanced_binding">Advanced Binding</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 101 | </li> |
| 102 | </ol> |
| 103 | </li> |
| 104 | |
| 105 | <li> |
| 106 | <a href="#attribute_setters">Attribute Setters</a> |
| 107 | <ol> |
| 108 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 109 | <a href="#automatic_setters">Automatic Setters</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 110 | </li> |
| 111 | |
| 112 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 113 | <a href="#renamed_setters">Renamed Setters</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 114 | </li> |
| 115 | |
| 116 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 117 | <a href="#custom_setters">Custom Setters</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 118 | </li> |
| 119 | </ol> |
| 120 | </li> |
| 121 | |
| 122 | <li> |
| 123 | <a href="#converters">Converters</a> |
| 124 | <ol> |
| 125 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 126 | <a href="#object_conversions">Object Conversions</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 127 | </li> |
| 128 | |
| 129 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 130 | <a href="#custom_conversions">Custom Conversions</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 131 | </li> |
| 132 | </ol> |
| 133 | </li> |
| 134 | </ol> |
| 135 | </div><!-- qv --> |
| 136 | </div><!-- qv-wrapper --> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 137 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 138 | <p> |
| 139 | This document explains how to use the Data Binding Library to write |
| 140 | declarative layouts and minimize the glue code necessary to bind your |
| 141 | application logic and layouts. |
| 142 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 143 | |
| Dirk Dougherty | 08710f1 | 2015-05-28 22:01:36 -0700 | [diff] [blame] | 144 | <p>The Data Binding Library offers both flexibility and broad comnpatibility |
| 145 | — it's a support library, so you can use it with all Android platform |
| 146 | versions back to <strong>Android 2.1</strong> (API level 7+).</p> |
| 147 | |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 148 | <p>To use data binding, Android Plugin for Gradle <strong>1.3.0-beta4</strong> |
| Dirk Dougherty | bcab182 | 2015-06-02 14:05:31 -0700 | [diff] [blame] | 149 | or higher is required.</p> |
| Dirk Dougherty | 08710f1 | 2015-05-28 22:01:36 -0700 | [diff] [blame] | 150 | |
| 151 | <h4>Beta release</h4> |
| 152 | |
| 153 | <div class="caution"> |
| 154 | <p>Please note that the Data Binding library is a <strong>beta release</strong>. |
| 155 | While Data Binding is in beta, developers should be aware of the following |
| 156 | caveats:</p> |
| 157 | <ul> |
| 158 | <li> |
| 159 | This is a beta release of the feature intended to generate developer |
| 160 | feedback. It might contain bugs, and it might not work for your use case, |
| 161 | so use it at your own risk. That said, we do want your feedback! Please |
| 162 | let us know what is or isn’t working for you using the <a |
| 163 | href="https://code.google.com/p/android-developer-preview/">issue |
| Dirk Dougherty | bcab182 | 2015-06-02 14:05:31 -0700 | [diff] [blame] | 164 | tracker</a>. |
| Dirk Dougherty | 08710f1 | 2015-05-28 22:01:36 -0700 | [diff] [blame] | 165 | </li> |
| 166 | <li> |
| 167 | The Data Binding library beta release is subject to significant changes, |
| 168 | including those which are not source code compatible with your app. That is, |
| 169 | significant rework may be required to take updates to the library in the future. |
| 170 | </li> |
| 171 | <li> |
| 172 | Developers should feel free to publish apps built with the Data Binding |
| 173 | library beta release, with the caveats that the standard Android SDK and |
| 174 | Google Play terms of service apply, and it’s always a great idea to test your |
| 175 | app thoroughly when adopting new libraries or tools. |
| 176 | </li> |
| 177 | <li> |
| 178 | We’re just getting started with Android Studio support at this time. |
| 179 | Further Android Studio support will come in the future. |
| 180 | </li> |
| 181 | <li> |
| Dirk Dougherty | bcab182 | 2015-06-02 14:05:31 -0700 | [diff] [blame] | 182 | By using the Data Binding library beta release, you acknowledge these |
| Dirk Dougherty | 08710f1 | 2015-05-28 22:01:36 -0700 | [diff] [blame] | 183 | caveats.</li> |
| 184 | </ul> |
| 185 | </div> |
| 186 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 187 | <h2 id="build_environment"> |
| 188 | Build Environment |
| 189 | </h2> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 190 | |
| Dirk Dougherty | 08710f1 | 2015-05-28 22:01:36 -0700 | [diff] [blame] | 191 | <p>To get started with Data Binding, download the library from the Support |
| 192 | repository in the Android SDK manager. </p> |
| 193 | |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 194 | <p>The Data Binding plugin requires Android Plugin for Gradle <strong>1.3.0-beta4 |
| 195 | or higher</strong>, so update your build dependencies (in the top-level |
| 196 | <code>build.gradle</code> file) as needed.</p> |
| Dirk Dougherty | 08710f1 | 2015-05-28 22:01:36 -0700 | [diff] [blame] | 197 | |
| Dirk Dougherty | bcab182 | 2015-06-02 14:05:31 -0700 | [diff] [blame] | 198 | <p>Also, make sure you are using a compatible version of Android Studio. |
| 199 | <strong>Android Studio 1.3</strong> adds the code-completion and layout-preview |
| 200 | support for data binding.</p> |
| Dirk Dougherty | 08710f1 | 2015-05-28 22:01:36 -0700 | [diff] [blame] | 201 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 202 | <p> |
| 203 | <strong>Setting Up Work Environment:</strong> |
| 204 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 205 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 206 | <p> |
| 207 | To set up your application to use data binding, add data binding to the class |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 208 | path of your top-level <code>build.gradle</code> file, right below "android". |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 209 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 210 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 211 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 212 | dependencies { |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 213 | classpath <strong>"com.android.tools.build:gradle:1.3.0-beta4"</strong> |
| 214 | classpath <strong>"com.android.databinding:dataBinder:1.0-rc1"</strong> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 215 | } |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 216 | </pre> |
| 217 | <p> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 218 | Then make sure jcenter is in the repositories list for your projects in the top-level |
| 219 | <code>build.gradle</code> file. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 220 | </p> |
| 221 | |
| 222 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 223 | allprojects { |
| 224 | repositories { |
| 225 | jcenter() |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 229 | <p> |
| 230 | In each module you want to use data binding, apply the plugin right after |
| 231 | android plugin |
| 232 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 233 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 234 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 235 | apply plugin: 'com.android.application' |
| 236 | apply plugin: 'com.android.databinding' |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 237 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 238 | <p> |
| 239 | The data binding plugin is going to add necessary <strong>provided</strong> |
| 240 | and <strong>compile configuration</strong> dependencies to your project. |
| 241 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 242 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 243 | <h2 id="data_binding_layout_files"> |
| 244 | Data Binding Layout Files |
| 245 | </h2> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 246 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 247 | <h3 id="writing_expressions"> |
| 248 | Writing your first data binding expressions |
| 249 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 250 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 251 | <p> |
| 252 | Data-binding layout files are slightly different and start with a root tag of |
| 253 | <strong>layout</strong> followed by a <strong>data</strong> element and a |
| 254 | <strong>view</strong> root element. This view element is what your root would |
| 255 | be in a non-binding layout file. A sample file looks like this: |
| 256 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 257 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 258 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 259 | <?xml version="1.0" encoding="utf-8"?> |
| 260 | <layout xmlns:android="http://schemas.android.com/apk/res/android"> |
| 261 | <data> |
| 262 | <variable name="user" type="com.example.User"/> |
| 263 | </data> |
| 264 | <LinearLayout |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 265 | android:orientation="vertical" |
| 266 | android:layout_width="match_parent" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 267 | android:layout_height="match_parent"> |
| 268 | <TextView android:layout_width="wrap_content" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 269 | android:layout_height="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 270 | android:text="@{user.firstName}"/> |
| 271 | <TextView android:layout_width="wrap_content" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 272 | android:layout_height="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 273 | android:text="@{user.lastName}"/> |
| 274 | </LinearLayout> |
| 275 | </layout> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 276 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 277 | <p> |
| 278 | The user <strong>variable</strong> within <strong>data</strong> describes a |
| 279 | property that may be used within this layout. |
| 280 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 281 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 282 | <pre> |
| 283 | <<strong>variable name="user" type="com.example.User"</strong>/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 284 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 285 | <p> |
| 286 | Expressions within the layout are written in the attribute properties using |
| 287 | the “<code>@{}</code>” syntax. Here, the TextView’s text is set to the |
| 288 | firstName property of user: |
| 289 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 290 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 291 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 292 | <TextView android:layout_width="wrap_content" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 293 | android:layout_height="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 294 | android:text="@{user.firstName}"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 295 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 296 | <h3 id="data_object"> |
| 297 | Data Object |
| 298 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 299 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 300 | <p> |
| 301 | Let’s assume for now that you have a plain-old Java object (POJO) for User: |
| 302 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 303 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 304 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 305 | public class User { |
| 306 | public final String firstName; |
| 307 | public final String lastName; |
| 308 | public User(String firstName, String lastName) { |
| 309 | this.firstName = firstName; |
| 310 | this.lastName = lastName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 314 | <p> |
| 315 | This type of object has data that never changes. It is common in applications |
| 316 | to have data that is read once and never changes thereafter. It is also |
| 317 | possible to use a JavaBeans objects: |
| 318 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 319 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 320 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 321 | public class User { |
| 322 | private final String firstName; |
| 323 | private final String lastName; |
| 324 | public User(String firstName, String lastName) { |
| 325 | this.firstName = firstName; |
| 326 | this.lastName = lastName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 327 | } |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 328 | public String getFirstName() { |
| 329 | return this.firstName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 330 | } |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 331 | public String getLastName() { |
| 332 | return this.lastName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 336 | <p> |
| 337 | From the perspective of data binding, these two classes are equivalent. The |
| 338 | expression <strong><code>@{user.firstName}</code></strong> used for |
| 339 | the TextView’s <strong><code>android:text</code></strong> attribute will |
| 340 | access the <strong><code>firstName</code></strong> field in the former class |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 341 | and the <code>getFirstName()</code> method in the latter class. Alternatively, it |
| 342 | will also be resolved to <code>firstName()</code> if that method exists. |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 343 | </p> |
| 344 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 345 | <h3 id="binding_data"> |
| 346 | Binding Data |
| 347 | </h3> |
| 348 | |
| 349 | <p> |
| 350 | By default, a Binding class will be generated based on the name of the layout |
| 351 | file, converting it to Pascal case and suffixing “Binding” to it. The above |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 352 | layout file was <code>main_activity.xml</code> so the generate class was |
| 353 | <code>MainActivityBinding</code>. This class holds all the bindings from the |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 354 | layout properties (e.g. the <code>user</code> variable) to the layout’s Views |
| 355 | and knows how to assign values for the binding expressions.The easiest means |
| 356 | for creating the bindings is to do it while inflating: |
| 357 | </p> |
| 358 | |
| 359 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 360 | @Override |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 361 | protected void onCreate(Bundle savedInstanceState) { |
| 362 | super.onCreate(savedInstanceState); |
| 363 | MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity); |
| 364 | User user = new User("Test", "User"); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 365 | binding.setUser(user); |
| 366 | } |
| 367 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 368 | <p> |
| 369 | You’re done! Run the application and you’ll see Test User in the UI. |
| 370 | Alternatively, you can get the view via: |
| 371 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 372 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 373 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 374 | MainActivityBinding binding = MainActivityBinding.<em>inflate</em>(getLayoutInflater()); |
| 375 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 376 | <p> |
| 377 | If you are using data binding items inside a ListView or RecyclerView |
| 378 | adapter, you may prefer to use: |
| 379 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 380 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 381 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 382 | ListItemBinding binding = ListItemBinding.inflate(layoutInflater, viewGroup, false); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 383 | //or |
| 384 | ListItemBinding binding = DataBindingUtil.<em>inflate</em>(layoutInflater, R.layout.<em><strong>list_item</strong></em>, viewGroup, <strong>false</strong>); |
| 385 | </pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 386 | |
| 387 | <h3 id="binding_events"> |
| 388 | Binding Events |
| 389 | </h3> |
| 390 | <p> |
| 391 | Events may be bound to handler methods directly, similar to the way |
| 392 | <strong><code>android:onClick</code></strong> can be assigned to a method in the Activity. |
| 393 | Event attribute names are governed by the name of the listener method with a few exceptions. |
| 394 | For example, {@link android.view.View.OnLongClickListener} has a method {@link android.view.View.OnLongClickListener#onLongClick onLongClick()}, |
| 395 | so the attribute for this event is <code>android:onLongClick</code>. |
| 396 | </p> |
| 397 | <p> |
| 398 | To assign an event to its handler, use a normal binding expression, with the value |
| 399 | being the method name to call. For example, if your data object has two methods: |
| 400 | </p> |
| 401 | <pre>public class MyHandlers { |
| 402 | public void onClickFriend(View view) { ... } |
| 403 | public void onClickEnemy(View view) { ... } |
| 404 | } |
| 405 | </pre> |
| 406 | <p> |
| 407 | The binding expression can assign the click listener for a View: |
| 408 | </p> |
| 409 | <pre> |
| 410 | <?xml version="1.0" encoding="utf-8"?> |
| 411 | <layout xmlns:android="http://schemas.android.com/apk/res/android"> |
| 412 | <data> |
| 413 | <variable name="handlers" type="com.example.Handlers"/> |
| 414 | <variable name="user" type="com.example.User"/> |
| 415 | </data> |
| 416 | <LinearLayout |
| 417 | android:orientation="vertical" |
| 418 | android:layout_width="match_parent" |
| 419 | android:layout_height="match_parent"> |
| 420 | <TextView android:layout_width="wrap_content" |
| 421 | android:layout_height="wrap_content" |
| 422 | android:text="@{user.firstName}" |
| 423 | android:onClick="@{user.isFriend ? handlers.onClickFriend : handlers.onClickEnemy}"/> |
| 424 | <TextView android:layout_width="wrap_content" |
| 425 | android:layout_height="wrap_content" |
| 426 | android:text="@{user.lastName}" |
| 427 | android:onClick="@{user.isFriend ? handlers.onClickFriend : handlers.onClickEnemy}"/> |
| 428 | </LinearLayout> |
| 429 | </layout> |
| 430 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 431 | <h2 id="layout_details"> |
| 432 | Layout Details |
| 433 | </h2> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 434 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 435 | <h3 id="imports"> |
| 436 | Imports |
| 437 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 438 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 439 | <p> |
| 440 | Zero or more <strong><code>import</code></strong> elements may be used inside |
| 441 | the <strong><code>data</code></strong> element. These allow easy reference to |
| 442 | classes inside your layout file, just like in Java. |
| 443 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 444 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 445 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 446 | <data> |
| 447 | <import type="android.view.View"/> |
| 448 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 449 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 450 | <p> |
| 451 | Now, View may be used within your binding expression: |
| 452 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 453 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 454 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 455 | <TextView |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 456 | android:text="@{user.lastName}" |
| 457 | android:layout_width="wrap_content" |
| 458 | android:layout_height="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 459 | android:visibility="@{user.isAdult ? View.VISIBLE : View.GONE}"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 460 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 461 | <p> |
| 462 | When there are class name conflicts, one of the classes may be renamed to an |
| 463 | “alias:” |
| 464 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 465 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 466 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 467 | <import type="android.view.View"/> |
| 468 | <import type="com.example.real.estate.View" |
| 469 | alias="Vista"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 470 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 471 | <p> |
| 472 | Now, <strong><code>Vista</code></strong> may be used to reference the |
| 473 | <code>com.example.real.estate.View</code> and |
| 474 | <strong><code>View</code></strong> may be used to reference |
| 475 | <code>android.view.View</code> within the layout file. Imported types may be |
| 476 | used as type references in variables and expressions: |
| 477 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 478 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 479 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 480 | <data> |
| 481 | <import type="com.example.User"/> |
| 482 | <import type="java.util.List"/> |
| 483 | <variable name="user" type="User"/> |
| 484 | <variable name="userList" type="List&lt;User>"/> |
| 485 | </data> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 486 | </pre> |
| Dirk Dougherty | 08710f1 | 2015-05-28 22:01:36 -0700 | [diff] [blame] | 487 | <p class="caution"> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 488 | <strong>Note</strong>: Android Studio does not yet handle imports so the |
| 489 | autocomplete for imported variables may not work in your IDE. Your |
| 490 | application will still compile fine and you can work around the IDE issue by |
| 491 | using fully qualified names in your variable definitions. |
| 492 | </p> |
| 493 | |
| 494 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 495 | <TextView |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 496 | android:text="@{((User)(user.connection)).lastName}" |
| 497 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 498 | android:layout_height="wrap_content"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 499 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 500 | <p> |
| 501 | Imported types may also be used when referencing static fields and methods in |
| 502 | expressions: |
| 503 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 504 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 505 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 506 | <data> |
| 507 | <import type="com.example.MyStringUtils"/> |
| 508 | <variable name="user" type="com.example.User"/> |
| 509 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 510 | … |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 511 | <TextView |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 512 | android:text="@{MyStringUtils.capitalize(user.lastName)}" |
| 513 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 514 | android:layout_height="wrap_content"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 515 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 516 | <p> |
| 517 | Just as in Java, <code>java.lang.*</code> is imported automatically. |
| 518 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 519 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 520 | <h3 id="variables"> |
| 521 | Variables |
| 522 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 523 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 524 | <p> |
| 525 | Any number of <strong><code>variable</code></strong> elements may be used |
| 526 | inside the <strong><code>data</code></strong> element. Each |
| 527 | <strong><code>variable</code></strong> element describes a property that may |
| 528 | be set on the layout to be used in binding expressions within the layout |
| 529 | file. |
| 530 | </p> |
| 531 | |
| 532 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 533 | <data> |
| 534 | <import type="android.graphics.drawable.Drawable"/> |
| 535 | <variable name="user" type="com.example.User"/> |
| 536 | <variable name="image" type="Drawable"/> |
| 537 | <variable name="note" type="String"/> |
| 538 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 539 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 540 | <p> |
| 541 | The variable types are inspected at compile time, so if a variable implements |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 542 | {@link android.databinding.Observable} or is an <a href= |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 543 | "#observable_collections">observable collection</a>, that should be reflected |
| 544 | in the type. If the variable is a base class or interface that does not |
| 545 | implement the Observable* interface, the variables will <strong>not |
| 546 | be</strong> observed! |
| 547 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 548 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 549 | <p> |
| 550 | When there are different layout files for various configurations (e.g. |
| 551 | landscape or portrait), the variables will be combined. There must not be |
| 552 | conflicting variable definitions between these layout files. |
| 553 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 554 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 555 | <p> |
| 556 | The generated binding class will have a setter and getter for each of the |
| 557 | described variables. The variables will take the default Java values until |
| 558 | the setter is called — <code>null</code> for reference types, |
| 559 | <code>0</code> for <code>int</code>, <code>false</code> for |
| 560 | <code>boolean</code>, etc. |
| 561 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 562 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 563 | <h3 id="custom_binding_class_names"> |
| 564 | Custom Binding Class Names |
| 565 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 566 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 567 | <p> |
| 568 | By default, a Binding class is generated based on the name of the layout |
| 569 | file, starting it with upper-case, removing underscores ( _ ) and |
| 570 | capitalizing the following letter and then suffixing “Binding”. This class |
| 571 | will be placed in a databinding package under the module package. For |
| 572 | example, the layout file <code>contact_item.xml</code> will generate |
| 573 | <code>ContactItemBinding</code>. If the module package is |
| 574 | <code>com.example.my.app</code>, then it will be placed in |
| 575 | <code>com.example.my.app.databinding</code>. |
| 576 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 577 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 578 | <p> |
| 579 | Binding classes may be renamed or placed in different packages by adjusting |
| 580 | the <strong><code>class</code></strong> attribute of the |
| 581 | <strong><code>data</code></strong> element. For example: |
| 582 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 583 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 584 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 585 | <data class="ContactItem"> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 586 | ... |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 587 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 588 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 589 | <p> |
| 590 | This generates the binding class as <code>ContactItem</code> in the |
| 591 | databinding package in the module package. If the class should be generated |
| 592 | in a different package within the module package, it may be prefixed with |
| 593 | “.”: |
| 594 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 595 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 596 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 597 | <data class=".ContactItem"> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 598 | ... |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 599 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 600 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 601 | <p> |
| 602 | In this case, <code>ContactItem</code> is generated in the module package |
| 603 | directly. Any package may be used if the full package is provided: |
| 604 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 605 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 606 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 607 | <data class="com.example.ContactItem"> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 608 | ... |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 609 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 610 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 611 | <h3 id="includes"> |
| 612 | Includes |
| 613 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 614 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 615 | <p> |
| 616 | Variables may be passed into an included layout's binding from the |
| 617 | containing layout by using the application namespace and the variable name in |
| 618 | an attribute: |
| 619 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 620 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 621 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 622 | <?xml version="1.0" encoding="utf-8"?> |
| 623 | <layout xmlns:android="http://schemas.android.com/apk/res/android" |
| 624 | xmlns:bind="http://schemas.android.com/apk/res-auto"> |
| 625 | <data> |
| 626 | <variable name="user" type="com.example.User"/> |
| 627 | </data> |
| 628 | <LinearLayout |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 629 | android:orientation="vertical" |
| 630 | android:layout_width="match_parent" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 631 | android:layout_height="match_parent"> |
| 632 | <include layout="@layout/name" |
| 633 | bind:user="@{user}"/> |
| 634 | <include layout="@layout/contact" |
| 635 | bind:user="@{user}"/> |
| 636 | </LinearLayout> |
| 637 | </layout> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 638 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 639 | <p> |
| 640 | Here, there must be a <code>user</code> variable in both the |
| 641 | <code>name.xml</code> and <code>contact.xml</code> layout files. |
| 642 | </p> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 643 | <p> |
| 644 | Data binding does not support include as a direct child of a merge element. For example, |
| 645 | <strong>the following layout is not supported:</strong> |
| 646 | </p> |
| 647 | <pre> |
| 648 | <?xml version="1.0" encoding="utf-8"?> |
| 649 | <layout xmlns:android="http://schemas.android.com/apk/res/android" |
| 650 | xmlns:bind="http://schemas.android.com/apk/res-auto"> |
| 651 | <data> |
| 652 | <variable name="user" type="com.example.User"/> |
| 653 | </data> |
| 654 | <merge> |
| 655 | <include layout="@layout/name" |
| 656 | bind:user="@{user}"/> |
| 657 | <include layout="@layout/contact" |
| 658 | bind:user="@{user}"/> |
| 659 | </merge> |
| 660 | </layout> |
| 661 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 662 | <h3 id="expression_language"> |
| 663 | Expression Language |
| 664 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 665 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 666 | <h4 id="common_features"> |
| 667 | Common Features |
| 668 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 669 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 670 | <p> |
| 671 | The expression language looks a lot like a Java expression. These are the |
| 672 | same: |
| 673 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 674 | |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 675 | <ul> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 676 | <li>Mathematical <strong><code>+ - / * %</code></strong> |
| 677 | </li> |
| 678 | |
| 679 | <li>String concatenation <strong><code>+</code></strong> |
| 680 | </li> |
| 681 | |
| 682 | <li> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 683 | Logical <strong><code>&& ||</code></strong> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 684 | </li> |
| 685 | |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 686 | <li>Binary <strong><code>& | ^</code></strong> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 687 | </li> |
| 688 | |
| 689 | <li>Unary <strong><code>+ - ! ~</code></strong> |
| 690 | </li> |
| 691 | |
| 692 | <li>Shift <strong><code>>> >>> <<</code></strong> |
| 693 | </li> |
| 694 | |
| 695 | <li>Comparison <strong><code>== > < >= <=</code></strong> |
| 696 | </li> |
| 697 | |
| 698 | <li> |
| 699 | <strong><code>instanceof</code></strong> |
| 700 | </li> |
| 701 | |
| 702 | <li>Grouping <strong><code>()</code></strong> |
| 703 | </li> |
| 704 | |
| 705 | <li>Literals - character, String, numeric, <strong><code>null</code></strong> |
| 706 | </li> |
| 707 | |
| 708 | <li>Cast |
| 709 | </li> |
| 710 | |
| 711 | <li>Method calls |
| 712 | </li> |
| 713 | |
| 714 | <li>Field access |
| 715 | </li> |
| 716 | |
| 717 | <li>Array access <strong><code>[]</code></strong> |
| 718 | </li> |
| 719 | |
| 720 | <li>Ternary operator <strong><code>?:</code></strong> |
| 721 | </li> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 722 | </ul> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 723 | |
| 724 | <p> |
| 725 | Examples: |
| 726 | </p> |
| 727 | |
| 728 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 729 | android:text="@{String.valueOf(index + 1)}" |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 730 | android:visibility="@{age &lt; 13 ? View.GONE : View.VISIBLE}" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 731 | android:transitionName='@{"image_" + id}' |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 732 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 733 | <h4 id="missing_operations"> |
| 734 | Missing Operations |
| 735 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 736 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 737 | <p> |
| 738 | A few operations are missing from the expression syntax that you can use in |
| 739 | Java. |
| 740 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 741 | |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 742 | <ul> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 743 | <li> |
| 744 | <strong><code>this</code></strong> |
| 745 | </li> |
| 746 | |
| 747 | <li> |
| 748 | <strong><code>super</code></strong> |
| 749 | </li> |
| 750 | |
| 751 | <li> |
| 752 | <strong><code>new</code></strong> |
| 753 | </li> |
| 754 | |
| 755 | <li>Explicit generic invocation |
| 756 | </li> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 757 | </ul> |
| 758 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 759 | <h4 id="null_coalescing_operator"> |
| 760 | Null Coalescing Operator |
| 761 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 762 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 763 | <p> |
| 764 | The null coalescing operator (<strong><code>??</code></strong>) chooses the |
| 765 | left operand if it is not null or the right if it is null. |
| 766 | </p> |
| 767 | |
| 768 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 769 | <strong>android:text="@{user.displayName ?? user.lastName}"</strong> |
| 770 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 771 | <p> |
| 772 | This is functionally equivalent to: |
| 773 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 774 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 775 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 776 | <strong>android:text="@{user.displayName != null ? user.displayName : user.lastName}"</strong> |
| 777 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 778 | <h4 id="property_reference"> |
| 779 | Property Reference |
| 780 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 781 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 782 | <p> |
| 783 | The first was already discussed in the <a href= |
| 784 | "#writing_your_first_data_binding_expressions">Writing your first data |
| 785 | binding expressions</a> above: short form JavaBean references. When an |
| 786 | expression references a property on a class, it uses the same format for |
| 787 | fields, getters, and ObservableFields. |
| 788 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 789 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 790 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 791 | <strong>android:text="@{user.lastName}"</strong> |
| 792 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 793 | <h4> |
| 794 | Avoiding NullPointerException |
| 795 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 796 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 797 | <p> |
| 798 | Generated data binding code automatically checks for nulls and avoid null |
| 799 | pointer exceptions. For example, in the expression |
| 800 | <code>@{user.name}</code>, if <code>user</code> is null, |
| 801 | <code>user.name</code> will be assigned its default value (null). If you were |
| 802 | referencing <code>user.age</code>, where age is an <code>int</code>, then it |
| 803 | would default to 0. |
| 804 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 805 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 806 | <h4 id="collections"> |
| 807 | Collections |
| 808 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 809 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 810 | <p> |
| 811 | Common collections: arrays, lists, sparse lists, and maps, may be accessed |
| 812 | using the <code>[]</code> operator for convenience. |
| 813 | </p> |
| 814 | |
| 815 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 816 | <data> |
| 817 | <import type="android.util.SparseArray"/> |
| 818 | <import type="java.util.Map"/> |
| 819 | <import type="java.util.List"/> |
| 820 | <variable name="list" type="List&lt;String>"/> |
| 821 | <variable name="sparse" type="SparseArray&lt;String>"/> |
| 822 | <variable name="map" type="Map&lt;String, String>"/> |
| 823 | <variable name="index" type="int"/> |
| 824 | <variable name="key" type="String"/> |
| 825 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 826 | … |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 827 | android:text="@{list[index]}" |
| 828 | … |
| 829 | android:text="@{sparse[index]}" |
| 830 | … |
| 831 | android:text="@{map[key]}" |
| 832 | |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 833 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 834 | <h4 id="string_literals"> |
| 835 | String Literals |
| 836 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 837 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 838 | <p> |
| 839 | When using single quotes around the attribute value, it is easy to use double |
| 840 | quotes in the expression: |
| 841 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 842 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 843 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 844 | android:text='@{map["firstName"]}' |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 845 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 846 | <p> |
| 847 | It is also possible to use double quotes to surround the attribute value. |
| 848 | When doing so, String literals should either use the &quot; or back quote |
| 849 | (`). |
| 850 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 851 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 852 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 853 | android:text="@{map[`firstName`}" |
| 854 | android:text="@{map[&quot;firstName&quot;]}" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 855 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 856 | <h4 id="resources"> |
| 857 | Resources |
| 858 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 859 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 860 | <p> |
| 861 | It is possible to access resources as part of expressions using the normal |
| 862 | syntax: |
| 863 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 864 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 865 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 866 | android:padding="@{large? @dimen/largePadding : @dimen/smallPadding}" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 867 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 868 | <p> |
| 869 | Format strings and plurals may be evaluated by providing parameters: |
| 870 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 871 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 872 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 873 | android:text="@{@string/nameFormat(firstName, lastName)}" |
| 874 | android:text="@{@plurals/banana(bananaCount)}" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 875 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 876 | <p> |
| 877 | When a plural takes multiple parameters, all parameters should be passed: |
| 878 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 879 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 880 | <pre> |
| 881 | |
| 882 | Have an orange |
| 883 | Have %d oranges |
| 884 | |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 885 | android:text="@{@plurals/orange(orangeCount, orangeCount)}" |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 886 | </pre> |
| 887 | <p> |
| 888 | Some resources require explicit type evaluation. |
| 889 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 890 | |
| 891 | <table> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 892 | <tr> |
| 893 | <th> |
| 894 | Type |
| 895 | </th> |
| 896 | <th> |
| 897 | Normal Reference |
| 898 | </th> |
| 899 | <th> |
| 900 | Expression Reference |
| 901 | </th> |
| 902 | </tr> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 903 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 904 | <tr> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 905 | <td> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 906 | String[] |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 907 | </td> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 908 | <td> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 909 | @array |
| 910 | </td> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 911 | <td> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 912 | @stringArray |
| 913 | </td> |
| 914 | </tr> |
| 915 | |
| 916 | <tr> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 917 | <td> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 918 | int[] |
| 919 | </td> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 920 | <td> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 921 | @array |
| 922 | </td> |
| 923 | <td> |
| 924 | @intArray |
| 925 | </td> |
| 926 | </tr> |
| 927 | |
| 928 | <tr> |
| 929 | <td> |
| 930 | TypedArray |
| 931 | </td> |
| 932 | <td> |
| 933 | @array |
| 934 | </td> |
| 935 | <td> |
| 936 | @typedArray |
| 937 | </td> |
| 938 | </tr> |
| 939 | |
| 940 | <tr> |
| 941 | <td> |
| 942 | Animator |
| 943 | </td> |
| 944 | <td> |
| 945 | @animator |
| 946 | </td> |
| 947 | <td> |
| 948 | @animator |
| 949 | </td> |
| 950 | </tr> |
| 951 | |
| 952 | <tr> |
| 953 | <td> |
| 954 | StateListAnimator |
| 955 | </td> |
| 956 | <td> |
| 957 | @animator |
| 958 | </td> |
| 959 | <td> |
| 960 | @stateListAnimator |
| 961 | </td> |
| 962 | </tr> |
| 963 | |
| 964 | <tr> |
| 965 | <td> |
| 966 | color <code>int</code> |
| 967 | </td> |
| 968 | <td> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 969 | @color |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 970 | </td> |
| 971 | <td> |
| 972 | @color |
| 973 | </td> |
| 974 | </tr> |
| 975 | |
| 976 | <tr> |
| 977 | <td> |
| 978 | ColorStateList |
| 979 | </td> |
| 980 | <td> |
| 981 | @color |
| 982 | </td> |
| 983 | <td> |
| 984 | @colorStateList |
| 985 | </td> |
| 986 | </tr> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 987 | </table> |
| 988 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 989 | <h2 id="data_objects"> |
| 990 | Data Objects |
| 991 | </h2> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 992 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 993 | <p> |
| 994 | Any plain old Java object (POJO) may be used for data binding, but modifying |
| 995 | a POJO will not cause the UI to update. The real power of data binding can be |
| 996 | used by giving your data objects the ability to notify when data changes. |
| 997 | There are three different data change notification mechanisms, |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 998 | <a href="#observable_objects">Observable objects</a>, |
| 999 | <a href="#observablefields">observable fields</a>, and |
| 1000 | <a href="#observable_collections">observable collection</a>s. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1001 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1002 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1003 | <p> |
| 1004 | When one of these observable data object is bound to the UI and a property of |
| 1005 | the data object changes, the UI will be updated automatically. |
| 1006 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1007 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1008 | <h3 id="observable_objects"> |
| 1009 | Observable Objects |
| 1010 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1011 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1012 | <p> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1013 | A class implementing the {@link android.databinding.Observable} interface |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1014 | will allow the binding to attach a single listener to a bound object to |
| 1015 | listen for changes of all properties on that object. |
| 1016 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1017 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1018 | <p> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1019 | The {@link android.databinding.Observable} interface has a mechanism to add and remove |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1020 | listeners, but notifying is up to the developer. To make development easier, |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1021 | a base class, {@link android.databinding.BaseObservable}, was created to implement the |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1022 | listener registration mechanism. The data class implementer is still |
| 1023 | responsible for notifying when the properties change. This is done by |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1024 | assigning a {@link android.databinding.Bindable} annotation to the getter and notifying in |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1025 | the setter. |
| 1026 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1027 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1028 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1029 | private static class User extends BaseObservable { |
| 1030 | private String firstName; |
| 1031 | private String lastName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1032 | @Bindable |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1033 | public String getFirstName() { |
| 1034 | return this.firstName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1035 | } |
| 1036 | @Bindable |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1037 | public String getLastName() { |
| 1038 | return this.lastName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1039 | } |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1040 | public void setFirstName(String firstName) { |
| 1041 | this.firstName = firstName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1042 | notifyPropertyChanged(BR.firstName); |
| 1043 | } |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1044 | public void setLastName(String lastName) { |
| 1045 | this.lastName = lastName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1046 | notifyPropertyChanged(BR.lastName); |
| 1047 | } |
| 1048 | } |
| 1049 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1050 | <p> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1051 | The {@link android.databinding.Bindable} annotation generates an entry in the BR class file |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1052 | during compilation. The BR class file will be generated in the module |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1053 | package. If the base class for data classes cannot be changed, the |
| 1054 | {@link android.databinding.Observable} interface may be implemented using the convenient |
| 1055 | {@link android.databinding.PropertyChangeRegistry} to store and notify listeners |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1056 | efficiently. |
| 1057 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1058 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1059 | <h3 id="observablefields"> |
| 1060 | ObservableFields |
| 1061 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1062 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1063 | <p> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1064 | A little work is involved in creating {@link android.databinding.Observable} classes, so |
| 1065 | developers who want to save time or have few properties may use |
| 1066 | {@link android.databinding.ObservableField} and its siblings |
| 1067 | {@link android.databinding.ObservableBoolean}, |
| 1068 | {@link android.databinding.ObservableByte}, |
| 1069 | {@link android.databinding.ObservableChar}, |
| 1070 | {@link android.databinding.ObservableShort}, |
| 1071 | {@link android.databinding.ObservableInt}, |
| 1072 | {@link android.databinding.ObservableLong}, |
| 1073 | {@link android.databinding.ObservableFloat}, |
| 1074 | {@link android.databinding.ObservableDouble}, and |
| 1075 | {@link android.databinding.ObservableParcelable}. |
| 1076 | <code>ObservableFields</code> are self-contained observable objects that have a single |
| 1077 | field. The primitive versions avoid boxing and unboxing during access operations. |
| 1078 | To use, create a public final field in the data class: |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1079 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1080 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1081 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1082 | private static class User { |
| 1083 | public final ObservableField<String> firstName = |
| 1084 | new ObservableField<>(); |
| 1085 | public final ObservableField<String> lastName = |
| 1086 | new ObservableField<>(); |
| 1087 | public final ObservableInt age = new ObservableInt(); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1088 | } |
| 1089 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1090 | <p> |
| 1091 | That's it! To access the value, use the set and get accessor methods: |
| 1092 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1093 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1094 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1095 | user.firstName.set("Google"); |
| 1096 | int age = user.age.get(); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1097 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1098 | <h3 id="observable_collections"> |
| 1099 | Observable Collections |
| 1100 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1101 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1102 | <p> |
| 1103 | Some applications use more dynamic structures to hold data. Observable |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1104 | collections allow keyed access to these data objects. |
| 1105 | {@link android.databinding.ObservableArrayMap} is |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1106 | useful when the key is a reference type, such as String. |
| 1107 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1108 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1109 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1110 | ObservableArrayMap<String, Object> user = new ObservableArrayMap<>(); |
| 1111 | user.put("firstName", "Google"); |
| 1112 | user.put("lastName", "Inc."); |
| 1113 | user.put("age", 17); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1114 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1115 | <p> |
| 1116 | In the layout, the map may be accessed through the String keys: |
| 1117 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1118 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1119 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1120 | <data> |
| 1121 | <import type="android.databinding.ObservableMap"/> |
| 1122 | <variable name="user" type="ObservableMap&lt;String, Object>"/> |
| 1123 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1124 | … |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1125 | <TextView |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1126 | android:text='@{user["lastName"]}' |
| 1127 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1128 | android:layout_height="wrap_content"/> |
| 1129 | <TextView |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1130 | android:text='@{String.valueOf(1 + (Integer)user["age"])}' |
| 1131 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1132 | android:layout_height="wrap_content"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1133 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1134 | <p> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1135 | {@link android.databinding.ObservableArrayList} is useful when the key is an integer: |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1136 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1137 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1138 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1139 | ObservableArrayList<Object> user = new ObservableArrayList<>(); |
| 1140 | user.add("Google"); |
| 1141 | user.add("Inc."); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1142 | user.add(17); |
| 1143 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1144 | <p> |
| 1145 | In the layout, the list may be accessed through the indices: |
| 1146 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1147 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1148 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1149 | <data> |
| 1150 | <import type="android.databinding.ObservableList"/> |
| 1151 | <import type="com.example.my.app.Fields"/> |
| 1152 | <variable name="user" type="ObservableList&lt;Object>"/> |
| 1153 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1154 | … |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1155 | <TextView |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1156 | android:text='@{user[Fields.LAST_NAME]}' |
| 1157 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1158 | android:layout_height="wrap_content"/> |
| 1159 | <TextView |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1160 | android:text='@{String.valueOf(1 + (Integer)user[Fields.AGE])}' |
| 1161 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1162 | android:layout_height="wrap_content"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1163 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1164 | <h2 id="generated_binding"> |
| 1165 | Generated Binding |
| 1166 | </h2> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1167 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1168 | <p> |
| 1169 | The generated binding class links the layout variables with the Views within |
| 1170 | the layout. As discussed earlier, the name and package of the Binding may be |
| 1171 | <a href="#custom_binding_class_names">customized</a>. The Generated binding |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1172 | classes all extend {@link android.databinding.ViewDataBinding}. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1173 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1174 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1175 | <h3 id="creating"> |
| 1176 | Creating |
| 1177 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1178 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1179 | <p> |
| 1180 | The binding should be created soon after inflation to ensure that the View |
| 1181 | hierarchy is not disturbed prior to binding to the Views with expressions |
| 1182 | within the layout. There are a few ways to bind to a layout. The most common |
| 1183 | is to use the static methods on the Binding class.The inflate method inflates |
| 1184 | the View hierarchy and binds to it all it one step. There is a simpler |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1185 | version that only takes a {@link android.view.LayoutInflater} and one that takes a |
| 1186 | {@link android.view.ViewGroup} as well: |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1187 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1188 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1189 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1190 | MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater); |
| 1191 | MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater, viewGroup, false); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1192 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1193 | <p> |
| 1194 | If the layout was inflated using a different mechanism, it may be bound |
| 1195 | separately: |
| 1196 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1197 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1198 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1199 | MyLayoutBinding binding = MyLayoutBinding.bind(viewRoot); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1200 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1201 | <p> |
| 1202 | Sometimes the binding cannot be known in advance. In such cases, the binding |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1203 | can be created using the {@link android.databinding.DataBindingUtil} class: |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1204 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1205 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1206 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1207 | ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater, layoutId, |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1208 | parent, attachToParent); |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1209 | ViewDataBinding binding = DataBindingUtil.bindTo(viewRoot, layoutId); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1210 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1211 | <h3 id="views_with_ids"> |
| 1212 | Views With IDs |
| 1213 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1214 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1215 | <p> |
| 1216 | A public final field will be generated for each View with an ID in the |
| 1217 | layout. The binding does a single pass on the View hierarchy, extracting the |
| 1218 | Views with IDs. This mechanism can be faster than calling findViewById for |
| 1219 | several Views. For example: |
| 1220 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1221 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1222 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1223 | <layout xmlns:android="http://schemas.android.com/apk/res/android"> |
| 1224 | <data> |
| 1225 | <variable name="user" type="com.example.User"/> |
| 1226 | </data> |
| 1227 | <LinearLayout |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1228 | android:orientation="vertical" |
| 1229 | android:layout_width="match_parent" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1230 | android:layout_height="match_parent"> |
| 1231 | <TextView android:layout_width="wrap_content" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1232 | android:layout_height="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1233 | android:text="@{user.firstName}" |
| 1234 | android:id="@+id/firstName"/> |
| 1235 | <TextView android:layout_width="wrap_content" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1236 | android:layout_height="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1237 | android:text="@{user.lastName}" |
| 1238 | android:id="@+id/lastName"/> |
| 1239 | </LinearLayout> |
| 1240 | </layout> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1241 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1242 | <p> |
| 1243 | Will generate a binding class with: |
| 1244 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1245 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1246 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1247 | public final TextView firstName; |
| 1248 | public final TextView lastName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1249 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1250 | <p> |
| 1251 | IDs are not nearly as necessary as without data binding, but there are still |
| 1252 | some instances where access to Views are still necessary from code. |
| 1253 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1254 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1255 | <h3 id="variables2"> |
| 1256 | Variables |
| 1257 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1258 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1259 | <p> |
| 1260 | Each variable will be given accessor methods. |
| 1261 | </p> |
| 1262 | |
| 1263 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1264 | <data> |
| 1265 | <import type="android.graphics.drawable.Drawable"/> |
| 1266 | <variable name="user" type="com.example.User"/> |
| 1267 | <variable name="image" type="Drawable"/> |
| 1268 | <variable name="note" type="String"/> |
| 1269 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1270 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1271 | <p> |
| 1272 | will generate setters and getters in the binding: |
| 1273 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1274 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1275 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1276 | public abstract com.example.User getUser(); |
| 1277 | public abstract void setUser(com.example.User user); |
| 1278 | public abstract Drawable getImage(); |
| 1279 | public abstract void setImage(Drawable image); |
| 1280 | public abstract String getNote(); |
| 1281 | public abstract void setNote(String note); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1282 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1283 | <h3 id="viewstubs"> |
| 1284 | ViewStubs |
| 1285 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1286 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1287 | <p> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1288 | {@link android.view.ViewStub}s are a little different from normal Views. They start off invisible |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1289 | and when they either are made visible or are explicitly told to inflate, they |
| 1290 | replace themselves in the layout by inflating another layout. |
| 1291 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1292 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1293 | <p> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1294 | Because the <code>ViewStub</code> essentially disappears from the View hierarchy, the View |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1295 | in the binding object must also disappear to allow collection. Because the |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1296 | Views are final, a {@link android.databinding.ViewStubProxy} object takes the place of the |
| 1297 | <code>ViewStub</code>, giving the developer access to the ViewStub when it exists and also |
| 1298 | access to the inflated View hierarchy when the <code>ViewStub</code> has been inflated. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1299 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1300 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1301 | <p> |
| 1302 | When inflating another layout, a binding must be established for the new |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1303 | layout. Therefore, the <code>ViewStubProxy</code> must listen to the <code>ViewStub</code>'s |
| 1304 | {@link android.view.ViewStub.OnInflateListener} and establish the binding at that time. Since |
| 1305 | only one can exist, the <code>ViewStubProxy</code> allows the developer to set an |
| 1306 | <code>OnInflateListener</code> on it that it will call after establishing the binding. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1307 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1308 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1309 | <h3 id="advanced_binding"> |
| 1310 | Advanced Binding |
| 1311 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1312 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1313 | <h4 id="dynamic_variables"> |
| 1314 | Dynamic Variables |
| 1315 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1316 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1317 | <p> |
| 1318 | At times, the specific binding class won't be known. For example, a |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1319 | {@link android.support.v7.widget.RecyclerView.Adapter} operating against arbitrary layouts |
| 1320 | won't know the specific binding class. It still must assign the binding value during the |
| 1321 | {@link android.support.v7.widget.RecyclerView.Adapter#onBindViewHolder}. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1322 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1323 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1324 | <p> |
| 1325 | In this example, all layouts that the RecyclerView binds to have an "item" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1326 | variable. The <code>BindingHolder</code> has a <code>getBinding</code> method returning the |
| 1327 | {@link android.databinding.ViewDataBinding} base. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1328 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1329 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1330 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1331 | public void onBindViewHolder(BindingHolder holder, int position) { |
| 1332 | final T item = mItems.get(position); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1333 | holder.getBinding().setVariable(BR.item, item); |
| 1334 | holder.getBinding().executePendingBindings(); |
| 1335 | } |
| 1336 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1337 | <h4 id="immediate_binding"> |
| 1338 | Immediate Binding |
| 1339 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1340 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1341 | <p> |
| 1342 | When a variable or observable changes, the binding will be scheduled to |
| 1343 | change before the next frame. There are times, however, when binding must be |
| 1344 | executed immediately. To force execution, use the |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1345 | {@link android.databinding.ViewDataBinding#executePendingBindings()} method. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1346 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1347 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1348 | <h4> |
| 1349 | Background Thread |
| 1350 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1351 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1352 | <p> |
| 1353 | You can change your data model in a background thread as long as it is not a |
| 1354 | collection. Data binding will localize each variable / field while evaluating |
| 1355 | to avoid any concurrency issues. |
| 1356 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1357 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1358 | <h2 id="attribute_setters"> |
| 1359 | Attribute Setters |
| 1360 | </h2> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1361 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1362 | <p> |
| 1363 | Whenever a bound value changes, the generated binding class must call a |
| 1364 | setter method on the View with the binding expression. The data binding |
| 1365 | framework has ways to customize which method to call to set the value. |
| 1366 | </p> |
| 1367 | |
| 1368 | <h3 id="automatic_setters"> |
| 1369 | Automatic Setters |
| 1370 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1371 | For an attribute, data binding tries to find the method setAttribute. The |
| 1372 | namespace for the attribute does not matter, only the attribute name itself. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1373 | <p> |
| 1374 | For example, an expression associated with TextView's attribute |
| 1375 | <strong><code>android:text</code></strong> will look for a setText(String). |
| 1376 | If the expression returns an int, data binding will search for a setText(int) |
| 1377 | method. Be careful to have the expression return the correct type, casting if |
| 1378 | necessary. Note that data binding will work even if no attribute exists with |
| 1379 | the given name. You can then easily "create" attributes for any setter by |
| 1380 | using data binding. For example, support DrawerLayout doesn't have any |
| 1381 | attributes, but plenty of setters. You can use the automatic setters to use |
| 1382 | one of these. |
| 1383 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1384 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1385 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1386 | <android.support.v4.widget.<strong>DrawerLayout |
| 1387 | android:layout_width="wrap_content" |
| 1388 | android:layout_height="wrap_content" |
| 1389 | app:scrimColor="@{@color/scrim}" |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1390 | app:drawerListener="@{fragment.drawerListener}"/></strong> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1391 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1392 | <h3 id="renamed_setters"> |
| 1393 | Renamed Setters |
| 1394 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1395 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1396 | <p> |
| 1397 | Some attributes have setters that don't match by name. For these |
| 1398 | methods, an attribute may be associated with the setter through |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1399 | {@link android.databinding.BindingMethods} annotation. This must be associated with |
| 1400 | a class and contains {@link android.databinding.BindingMethod} annotations, one for |
| 1401 | each renamed method. For example, the <strong><code>android:tint</code></strong> attribute |
| 1402 | is really associated with {@link android.widget.ImageView#setImageTintList}, not |
| 1403 | <code>setTint</code>. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1404 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1405 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1406 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1407 | @BindingMethods({ |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1408 | @BindingMethod(type = "android.widget.ImageView", |
| 1409 | attribute = "android:tint", |
| 1410 | method = "setImageTintList"), |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1411 | }) |
| 1412 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1413 | <p> |
| 1414 | It is unlikely that developers will need to rename setters; the android |
| 1415 | framework attributes have already been implemented. |
| 1416 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1417 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1418 | <h3 id="custom_setters"> |
| 1419 | Custom Setters |
| 1420 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1421 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1422 | <p> |
| 1423 | Some attributes need custom binding logic. For example, there is no |
| 1424 | associated setter for the <strong><code>android:paddingLeft</code></strong> |
| 1425 | attribute. Instead, <code>setPadding(left, top, right, bottom)</code> exists. |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1426 | A static binding adapter method with the {@link android.databinding.BindingAdapter} |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1427 | annotation allows the developer to customize how a setter for an attribute is |
| 1428 | called. |
| 1429 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1430 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1431 | <p> |
| 1432 | The android attributes have already had <code>BindingAdapter</code>s created. |
| 1433 | For example, here is the one for <code>paddingLeft</code>: |
| 1434 | </p> |
| 1435 | |
| 1436 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1437 | @BindingAdapter("android:paddingLeft") |
| 1438 | public static void setPaddingLeft(View view, int padding) { |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1439 | view.setPadding(padding, |
| 1440 | view.getPaddingTop(), |
| 1441 | view.getPaddingRight(), |
| 1442 | view.getPaddingBottom()); |
| 1443 | } |
| 1444 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1445 | <p> |
| 1446 | Binding adapters are useful for other types of customization. For example, a |
| 1447 | custom loader can be called off-thread to load an image. |
| 1448 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1449 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1450 | <p> |
| 1451 | Developer-created binding adapters will override the data binding default |
| 1452 | adapters when there is a conflict. |
| 1453 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1454 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1455 | <p> |
| 1456 | You can also have adapters that receive multiple parameters. |
| 1457 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1458 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1459 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1460 | @BindingAdapter({"bind:imageUrl", "bind:error"}) |
| 1461 | public static void loadImage(ImageView view, String url, Drawable error) { |
| 1462 | Picasso.with(view.getContext()).load(url).error(error).into(view); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1463 | } |
| 1464 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1465 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1466 | <ImageView app:imageUrl=“@{venue.imageUrl}” |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1467 | app:error=“@{@drawable/venueError}”/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1468 | </pre> |
| 1469 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1470 | <p> |
| 1471 | This adapter will be called if both <strong>imageUrl</strong> and |
| 1472 | <strong>error</strong> are used for an ImageView and <em>imageUrl</em> is a |
| 1473 | string and <em>error</em> is a drawable. |
| 1474 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1475 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1476 | <ul> |
| 1477 | <li>Custom namespaces are ignored during matching. |
| 1478 | </li> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1479 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1480 | <li>You can also write adapters for android namespace. |
| 1481 | </li> |
| 1482 | </ul> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1483 | |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1484 | <p> |
| 1485 | Binding adapter methods may optionally take the old values in their handlers. A method |
| 1486 | taking old and new values should have all old values for the attributes come first, followed |
| 1487 | by the new values: |
| 1488 | </p> |
| 1489 | <pre> |
| 1490 | @BindingAdapter("android:paddingLeft") |
| 1491 | public static void setPaddingLeft(View view, int oldPadding, int newPadding) { |
| 1492 | if (oldPadding != newPadding) { |
| 1493 | view.setPadding(newPadding, |
| 1494 | view.getPaddingTop(), |
| 1495 | view.getPaddingRight(), |
| 1496 | view.getPaddingBottom()); |
| 1497 | } |
| 1498 | } |
| 1499 | </pre> |
| 1500 | <p> |
| 1501 | Event handlers may only be used with interfaces or abstract classes with one abstract method. |
| 1502 | For example: |
| 1503 | </p> |
| 1504 | <pre> |
| 1505 | @BindingAdapter("android:onLayoutChange") |
| 1506 | public static void setOnLayoutChangeListener(View view, View.OnLayoutChangeListener oldValue, |
| 1507 | View.OnLayoutChangeListener newValue) { |
| 1508 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { |
| 1509 | if (oldValue != null) { |
| 1510 | view.removeOnLayoutChangeListener(oldValue); |
| 1511 | } |
| 1512 | if (newValue != null) { |
| 1513 | view.addOnLayoutChangeListener(newValue); |
| 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | </pre> |
| 1518 | <p> |
| 1519 | When a listener has multiple methods, it must be split into multiple listeners. For example, |
| 1520 | {@link android.view.View.OnAttachStateChangeListener} has two methods: |
| 1521 | {@link android.view.View.OnAttachStateChangeListener#onViewAttachedToWindow onViewAttachedToWindow()} and |
| 1522 | {@link android.view.View.OnAttachStateChangeListener#onViewDetachedFromWindow onViewDetachedFromWindow()}. |
| 1523 | We must then create two interfaces to differentiate the attributes and handlers for them. |
| 1524 | </p> |
| 1525 | |
| 1526 | <pre> |
| 1527 | @TargetApi(VERSION_CODES.HONEYCOMB_MR1) |
| 1528 | public interface OnViewDetachedFromWindow { |
| 1529 | void onViewDetachedFromWindow(View v); |
| 1530 | } |
| 1531 | |
| 1532 | @TargetApi(VERSION_CODES.HONEYCOMB_MR1) |
| 1533 | public interface OnViewAttachedToWindow { |
| 1534 | void onViewAttachedToWindow(View v); |
| 1535 | } |
| 1536 | </pre> |
| 1537 | <p> |
| 1538 | Because changing one listener will also affect the other, we must have three different |
| 1539 | binding adapters, one for each attribute and one for both, should they both be set. |
| 1540 | </p> |
| 1541 | <pre> |
| 1542 | @BindingAdapter("android:onViewAttachedToWindow") |
| 1543 | public static void setListener(View view, OnViewAttachedToWindow attached) { |
| 1544 | setListener(view, null, attached); |
| 1545 | } |
| 1546 | |
| 1547 | @BindingAdapter("android:onViewDetachedFromWindow") |
| 1548 | public static void setListener(View view, OnViewDetachedFromWindow detached) { |
| 1549 | setListener(view, detached, null); |
| 1550 | } |
| 1551 | |
| 1552 | @BindingAdapter({"android:onViewDetachedFromWindow", "android:onViewAttachedToWindow"}) |
| 1553 | public static void setListener(View view, final OnViewDetachedFromWindow detach, |
| 1554 | final OnViewAttachedToWindow attach) { |
| 1555 | if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1) { |
| 1556 | final OnAttachStateChangeListener newListener; |
| 1557 | if (detach == null && attach == null) { |
| 1558 | newListener = null; |
| 1559 | } else { |
| 1560 | newListener = new OnAttachStateChangeListener() { |
| 1561 | @Override |
| 1562 | public void onViewAttachedToWindow(View v) { |
| 1563 | if (attach != null) { |
| 1564 | attach.onViewAttachedToWindow(v); |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | @Override |
| 1569 | public void onViewDetachedFromWindow(View v) { |
| 1570 | if (detach != null) { |
| 1571 | detach.onViewDetachedFromWindow(v); |
| 1572 | } |
| 1573 | } |
| 1574 | }; |
| 1575 | } |
| 1576 | final OnAttachStateChangeListener oldListener = ListenerUtil.trackListener(view, |
| 1577 | newListener, R.id.onAttachStateChangeListener); |
| 1578 | if (oldListener != null) { |
| 1579 | view.removeOnAttachStateChangeListener(oldListener); |
| 1580 | } |
| 1581 | if (newListener != null) { |
| 1582 | view.addOnAttachStateChangeListener(newListener); |
| 1583 | } |
| 1584 | } |
| 1585 | } |
| 1586 | </pre> |
| 1587 | <p> |
| 1588 | The above example is slightly more complicated than normal because View uses add and remove |
| 1589 | for the listener instead of a set method for {@link android.view.View.OnAttachStateChangeListener}. |
| 1590 | The <code>android.databinding.adapters.ListenerUtil</code> class helps keep track of the previous |
| 1591 | listeners so that they may be removed in the Binding Adaper. |
| 1592 | </p> |
| 1593 | <p> |
| 1594 | By annotating the interfaces <code>OnViewDetachedFromWindow</code> and |
| 1595 | <code>OnViewAttachedToWindow</code> with |
| 1596 | <code>@TargetApi(VERSION_CODES.HONEYCOMB_MR1)</code>, the data binding code |
| 1597 | generator knows that the listener should only be generated when running on Honeycomb MR1 |
| 1598 | and new devices, the same version supported by |
| 1599 | {@link android.view.View#addOnAttachStateChangeListener}. |
| 1600 | </p> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1601 | <h2 id="converters"> |
| 1602 | Converters |
| 1603 | </h2> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1604 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1605 | <h3 id="object_conversions"> |
| 1606 | Object Conversions |
| 1607 | </h3> |
| 1608 | |
| 1609 | <p> |
| 1610 | When an Object is returned from a binding expression, a setter will be chosen |
| 1611 | from the automatic, renamed, and custom setters. The Object will be cast to a |
| 1612 | parameter type of the chosen setter. |
| 1613 | </p> |
| 1614 | |
| 1615 | <p> |
| 1616 | This is a convenience for those using ObservableMaps to hold data. for |
| 1617 | example: |
| 1618 | </p> |
| 1619 | |
| 1620 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1621 | <TextView |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1622 | android:text='@{userMap["lastName"]}' |
| 1623 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1624 | android:layout_height="wrap_content"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1625 | </pre> |
| 1626 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1627 | <p> |
| 1628 | The <code>userMap</code> returns an Object and that Object will be automatically cast to |
| 1629 | parameter type found in the setter <code>setText(CharSequence)</code>. When there |
| 1630 | may be confusion about the parameter type, the developer will need |
| 1631 | to cast in the expression. |
| 1632 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1633 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1634 | <h3 id="custom_conversions">Custom Conversions</h3> |
| 1635 | |
| 1636 | <p> |
| 1637 | Sometimes conversions should be automatic between specific types. For |
| 1638 | example, when setting the background: |
| 1639 | </p> |
| 1640 | |
| 1641 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1642 | <View |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1643 | android:background="@{isError ? @color/red : @color/white}" |
| 1644 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1645 | android:layout_height="wrap_content"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1646 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1647 | <p> |
| 1648 | Here, the background takes a <code>Drawable</code>, but the color is an |
| 1649 | integer. Whenever a <code>Drawable</code> is expected and an integer is |
| 1650 | returned, the <code>int</code> should be converted to a |
| 1651 | <code>ColorDrawable</code>. This conversion is done using a static method |
| 1652 | with a BindingConversion annotation: |
| 1653 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1654 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1655 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1656 | @BindingConversion |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1657 | public static ColorDrawable convertColorToDrawable(int color) { |
| 1658 | return new ColorDrawable(color); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1659 | } |
| 1660 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1661 | <p> |
| 1662 | Note that conversions only happen at the setter level, so it is <strong>not |
| 1663 | allowed</strong> to mix types like this: |
| 1664 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1665 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1666 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1667 | <View |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1668 | android:background="@{isError ? @drawable/error : @color/white}" |
| 1669 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1670 | android:layout_height="wrap_content"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1671 | </pre> |