了解数据绑定 - Data Binding Library

原文地址:<%20layout%20xmlns:android=%22http:/schemas.android.com/apk/res/android%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20TextView%20android:layout_width=%22wrap_content%22%20%20%20%20%20%20%20%20%20%20%20%20android:layout_height=%22wrap_content%22%20%20%20%20%20%20%20%20%20%20%20%20android:text=%22@%7Buser.firstName%7D%22/%20%20%20%20%20%20%20%20TextView%20android:layout_width=%22wrap_content%22%20%20%20%20%20%20%20%20%20%20%20%20android:layout_height=%22wrap_content%22%20%20%20%20%20%20%20%20%20%20%20%20android:text=%22@%7Buser.lastName%7D%22/%20%20%20%20%20%20The%20user%20variable%20within%20data%20describes%20a%20property%20that%20may%20be%20used%20within%20this%20layout.%20%20%20Expressions%20within%20the%20layout%20are%20written%20in%20the%20attribute%20properties%20using%20the%20%22@%7B%7D%22%20syntax.%20Here,%20the%20TextView’s%20text%20is%20set%20to%20the%20firstName%20property%20of%20user:%20%20TextView%20android:layout_width=%22wrap_content%22%20%20%20%20%20%20%20%20%20%20%20android:layout_height=%22wrap_content%22%20%20%20%20%20%20%20%20%20%20%20android:text=%22@%7Buser.firstName%7D%22/%20Data%20Object%20Let’s%20assume%20for%20now%20that%20you%20have%20a%20plain-old%20Java%20object%20(POJO)%20for%20User:%20%20public%20class%20User%20%7B%20%20%20%20public%20final%20String%20firstName;%20%20%20%20public%20final%20String%20lastName;%20%20%20%20public%20User(String%20firstName,%20String%20lastName)%20%7B%20%20%20%20%20%20%20%20this.firstName%20=%20firstName;%20%20%20%20%20%20%20%20this.lastName%20=%20lastName;%20%20%20%20%7D%20%7D%20This%20type%20of%20object%20has%20data%20that%20never%20changes.%20It%20is%20common%20in%20applications%20to%20have%20data%20that%20is%20read%20once%20and%20never%20changes%20thereafter.%20It%20is%20also%20possible%20to%20use%20a%20JavaBeans%20objects:%20%20public%20class%20User%20%7B%20%20%20%20private%20final%20String%20firstName;%20%20%20%20private%20final%20String%20lastName;%20%20%20%20public%20User(String%20firstName,%20String%20lastName)%20%7B%20%20%20%20%20%20%20%20this.firstName%20=%20firstName;%20%20%20%20%20%20%20%20this.lastName%20=%20lastName;%20%20%20%20%7D%20%20%20%20public%20String%20getFirstName()%20%7B%20%20%20%20%20%20%20%20return%20this.firstName;%20%20%20%20%7D%20%20%20%20public%20String%20getLastName()%20%7B%20%20%20%20%20%20%20%20return%20this.lastName;%20%20%20%20%7D%20%7D%20From%20the%20perspective%20of%20data%20binding,%20these%20two%20classes%20are%20equivalent.%20The%20expression%20@%7Buser.firstName%7D%20used%20for%20the%20TextView’s%20android:text%20attribute%20will%20access%20the%20firstName%20field%20in%20the%20former%20class%20and%20the%20getFirstName()%20method%20in%20the%20latter%20class.%20Alternatively,%20it%20will%20also%20be%20resolved%20to%20firstName()%20if%20that%20method%20exists.%20%20Binding%20Data%20By%20default,%20a%20Binding%20class%20will%20be%20generated%20based%20on%20the%20name%20of%20the%20layout%20file,%20converting%20it%20to%20Pascal%20case%20and%20suffixing%20%22Binding%22%20to%20it.%20The%20above%20layout%20file%20was%20main_activity.xml%20so%20the%20generate%20class%20was%20MainActivityBinding.%20This%20class%20holds%20all%20the%20bindings%20from%20the%20layout%20properties%20(e.g.%20the%20user%20variable)%20to%20the%20layout’s%20Views%20and%20knows%20how%20to%20assign%20values%20for%20the%20binding%20expressions.The%20easiest%20means%20for%20creating%20the%20bindings%20is%20to%20do%20it%20while%20inflating:%20%20@Override%20protected%20void%20onCreate(Bundle%20savedInstanceState)%20%7B%20%20%20%20super.onCreate(savedInstanceState);%20%20%20%20MainActivityBinding%20binding%20=%20DataBindingUtil.setContentView(this,%20R.layout.main_activity);%20%20%20%20User%20user%20=%20new%20User(%22Test%22,%20%22User%22);%20%20%20%20binding.setUser(user);%20%7D%20You’re%20done!%20Run%20the%20application%20and%20you’ll%20see%20Test%20User%20in%20the%20UI.%20Alternatively,%20you%20can%20get%20the%20view%20via:%20%20MainActivityBinding%20binding%20=%20MainActivityBinding.inflate(getLayoutInflater());%20If%20you%20are%20using%20data%20binding%20items%20inside%20a%20ListView%20or%20RecyclerView%20adapter,%20you%20may%20prefer%20to%20use:%20%20ListItemBinding%20binding%20=%20ListItemBinding.inflate(layoutInflater,%20viewGroup,%20false);%20/or%20ListItemBinding%20binding%20=%20DataBindingUtil.inflate(layoutInflater,%20R.layout.list_item,%20viewGroup,%20false);%20Event%20Handling%20Data%20Binding%20allows%20you%20to%20write%20expressions%20handling%20events%20that%20are%20dispatched%20from%20the%20views%20(e.g.%20onClick).%20Event%20attribute%20names%20are%20governed%20by%20the%20name%20of%20the%20listener%20method%20with%20a%20few%20exceptions.%20For%20example,%20View.OnLongClickListener%20has%20a%20method%20onLongClick(),%20so%20the%20attribute%20for%20this%20event%20is%20android:onLongClick.%20There%20are%20two%20ways%20to%20handle%20an%20event.%20%20Method%20References:%20In%20your%20expressions,%20you%20can%20reference%20methods%20that%20conform%20to%20the%20signature%20of%20the%20listener%20method.%20When%20an%20expression%20evaluates%20to%20a%20method%20reference,%20Data%20Binding%20wraps%20the%20method%20reference%20and%20owner%20object%20in%20a%20listener,%20and%20sets%20that%20listener%20on%20the%20target%20view.%20If%20the%20expression%20evaluates%20to%20null,%20Data%20Binding%20does%20not%20create%20a%20listener%20and%20sets%20a%20null%20listener%20instead.%20Listener%20Bindings:%20These%20are%20lambda%20expressions%20that%20are%20evaluated%20when%20the%20event%20happens.%20Data%20Binding%20always%20creates%20a%20listener,%20which%20it%20sets%20on%20the%20view.%20When%20the%20event%20is%20dispatched,%20the%20listener%20evaluates%20the%20lambda%20expression.%20Method%20References%20Events%20can%20be%20bound%20to%20handler%20methods%20directly,%20similar%20to%20the%20way%20android:onClick%20can%20be%20assigned%20to%20a%20method%20in%20an%20Activity.%20One%20major%20advantage%20compared%20to%20the%20View#onClick%20attribute%20is%20that%20the%20expression%20is%20processed%20at%20compile%20time,%20so%20if%20the%20method%20does%20not%20exist%20or%20its%20signature%20is%20not%20correct,%20you%20receive%20a%20compile%20time%20error.%20%20The%20major%20difference%20between%20Method%20References%20and%20Listener%20Bindings%20is%20that%20the%20actual%20listener%20implementation%20is%20created%20when%20the%20data%20is%20bound,%20not%20when%20the%20event%20is%20triggered.%20If%20you%20prefer%20to%20evaluate%20the%20expression%20when%20the%20event%20happens,%20you%20should%20use%20listener%20binding.%20%20To%20assign%20an%20event%20to%20its%20handler,%20use%20a%20normal%20binding%20expression,%20with%20the%20value%20being%20the%20method%20name%20to%20call.%20For%20example,%20if%20your%20data%20object%20has%20two%20methods:%20%20public%20class%20MyHandlers%20%7B%20%20%20%20%20public%20void%20onClickFriend(View%20view)%20%7B%20…%20%7D%20%7D%20The%20binding%20expression%20can%20assign%20the%20click%20listener%20for%20a%20View:%20%20<?xml%20version=%221.0%22%20encoding=%22utf-8%22?>%20layout%20xmlns:android=%22http:/schemas.android.com/apk/res/android%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20TextView%20android:layout_width=%22wrap_content%22%20%20%20%20%20%20%20%20%20%20%20%20android:layout_height=%22wrap_content%22%20%20%20%20%20%20%20%20%20%20%20%20android:text=%22@%7Buser.firstName%7D%22%20%20%20%20%20%20%20%20%20%20%20%20android:onClick=%22@%7Bhandlers::onClickFriend%7D%22/%20%20%20%20%20%20Note%20that%20the%20signature%20of%20the%20method%20in%20the%20expression%20must%20exactly%20match%20the%20signature%20of%20the%20method%20in%20the%20Listener%20object.%20%20Listener%20Bindings%20Listener%20Bindings%20are%20binding%20expressions%20that%20run%20when%20an%20event%20happens.%20They%20are%20similar%20to%20method%20references,%20but%20they%20let%20you%20run%20arbitrary%20data%20binding%20expressions.%20This%20feature%20is%20available%20with%20Android%20Gradle%20Plugin%20for%20Gradle%20version%202.0%20and%20later.%20%20In%20method%20references,%20the%20parameters%20of%20the%20method%20must%20match%20the%20parameters%20of%20the%20event%20listener.%20In%20Listener%20Bindings,%20only%20your%20return%20value%20must%20match%20the%20expected%20return%20value%20of%20the%20listener%20(unless%20it%20is%20expecting%20void).%20For%20example,%20you%20can%20have%20a%20presenter%20class%20that%20has%20the%20following%20method:%20%20public%20class%20Presenter%20%7B%20%20%20%20%20public%20void%20onSaveClick(Task%20task)%7B%7D%20%7D%20Then%20you%20can%20bind%20the%20click%20event%20to%20your%20class%20as%20follows:%20%20%20<?xml%20version=%221.0%22%20encoding=%22utf-8%22?>%20%20%20layout%20xmlns:android=%22http:/schemas.android.com/apk/res/android%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Button%20android:layout_width=%22wrap_content%22%20android:layout_height=%22wrap_content%22%20%20%20%20%20%20%20%20%20%20%20android:onClick=%22@%7B()%20-%20presenter.onSaveClick(task)%7D%22%20/>%20%20%20%20%20%20%20%20%20%20%20Listeners%20are%20represented%20by%20lambda%20expressions%20that%20are%20allowed%20only%20as%20root%20elements%20of%20your%20expressions.%20When%20a%20callback%20is%20used%20in%20an%20expression,%20Data%20Binding%20automatically%20creates%20the%20necessary%20listener%20and%20registers%20for%20the%20event.%20When%20the%20view%20fires%20the%20event,%20Data%20Binding%20evaluates%20the%20given%20expression.%20As%20in%20regular%20binding%20expressions,%20you%20still%20get%20the%20null%20and%20thread%20safety%20of%20Data%20Binding%20while%20these%20listener%20expressions%20are%20being%20evaluated.%20%20Note%20that%20in%20the%20example%20above,%20we%20haven’t%20defined%20the%20view%20parameter%20that%20is%20passed%20into%20onClick(android.view.View).%20Listener%20bindings%20provide%20two%20choices%20for%20listener%20parameters:%20you%20can%20either%20ignore%20all%20parameters%20to%20the%20method%20or%20name%20all%20of%20them.%20If%20you%20prefer%20to%20name%20the%20parameters,%20you%20can%20use%20them%20in%20your%20expression.%20For%20example,%20the%20expression%20above%20could%20be%20written%20as:%20%20%20%20android:onClick=%22@%7B(view)%20->%20presenter.onSaveClick(task)%7D%22%20Or%20if%20you%20wanted%20to%20use%20the%20parameter%20in%20the%20expression,%20it%20could%20work%20as%20follows:%20public%20class%20Presenter%20%7B%20%20%20%20%20public%20void%20onSaveClick(View%20view,%20Task%20task)%7B%7D%20%7D%20%20%20android:onClick=%22@%7B(theView)%20->%20presenter.onSaveClick(theView,%20task)%7D%22%20You%20can%20use%20a%20lambda%20expression%20with%20more%20than%20one%20parameter:%20public%20class%20Presenter%20%7B%20%20%20%20%20public%20void%20onCompletedChanged(Task%20task,%20boolean%20completed)%7B%7D%20%7D%20%20%20CheckBox%20android:layout_width=%22wrap_content%22%20android:layout_height=%22wrap_content%22%20%20%20%20%20%20%20%20%20android:onCheckedChanged=%22@%7B(cb,%20isChecked)%20-%20presenter.completeChanged(task,%20isChecked)%7D%22%20/>%20If%20the%20event%20you%20are%20listening%20to%20returns%20a%20value%20whose%20type%20is%20not%20void,%20your%20expressions%20must%20return%20the%20same%20type%20of%20value%20as%20well.%20For%20example,%20if%20you%20want%20to%20listen%20for%20the%20long%20click%20event,%20your%20expression%20should%20return%20boolean.%20%20public%20class%20Presenter%20%7B%20%20%20%20%20public%20boolean%20onLongClick(View%20view,%20Task%20task)%7B%7D%20%7D%20%20%20android:onLongClick=%22@%7B(theView)%20->%20presenter.onLongClick(theView,%20task)%7D%22%20If%20the%20expression%20cannot%20be%20evaluated%20due%20to%20null%20objects,%20Data%20Binding%20returns%20the%20default%20Java%20value%20for%20that%20type.%20For%20example,%20null%20for%20reference%20types,%200%20for%20int,%20false%20for%20boolean,%20etc.%20%20If%20you%20need%20to%20use%20an%20expression%20with%20a%20predicate%20(e.g.%20ternary),%20you%20can%20use%20void%20as%20a%20symbol.%20%20%20%20android:onClick=%22@%7B(v)%20->%20v.isVisible()%20?%20doSomething()%20:%20void%7D%22%20Avoid%20Complex%20Listeners%20Listener%20expressions%20are%20very%20powerful%20and%20can%20make%20your%20code%20very%20easy%20to%20read.%20On%20the%20other%20hand,%20listeners%20containing%20complex%20expressions%20make%20your%20layouts%20hard%20to%20read%20and%20unmaintainable.%20These%20expressions%20should%20be%20as%20simple%20as%20passing%20available%20data%20from%20your%20UI%20to%20your%20callback%20method.%20You%20should%20implement%20any%20business%20logic%20inside%20the%20callback%20method%20that%20you%20invoked%20from%20the%20listener%20expression.%20Some%20specialized%20click%20event%20handlers%20exist%20and%20they%20need%20an%20attribute%20other%20than%20android:onClick%20to%20avoid%20a%20conflict.%20The%20following%20attributes%20have%20been%20created%20to%20avoid%20such%20conflicts:%20%20Class%09Listener%20Setter%09Attribute%20SearchView%09setOnSearchClickListener(View.OnClickListener)%09android:onSearchClick%20ZoomControls%09setOnZoomInClickListener(View.OnClickListener)%09android:onZoomIn%20ZoomControls%09setOnZoomOutClickListener(View.OnClickListener)%09android:onZoomOut%20Layout%20Details%20Imports%20Zero%20or%20more%20import%20elements%20may%20be%20used%20inside%20the%20data%20element.%20These%20allow%20easy%20reference%20to%20classes%20inside%20your%20layout%20file,%20just%20like%20in%20Java.%20%20%20%20%20%20%20%20%20Now,%20View%20may%20be%20used%20within%20your%20binding%20expression:%20%20TextView%20%20%20%20android:text=%22@%7Buser.lastName%7D%22%20%20%20%20android:layout_width=%22wrap_content%22%20%20%20%20android:layout_height=%22wrap_content%22%20%20%20%20android:visibility=%22@%7Buser.isAdult%20?%20View.VISIBLE%20:%20View.GONE%7D%22/%20When%20there%20are%20class%20name%20conflicts,%20one%20of%20the%20classes%20may%20be%20renamed%20to%20an%20%22alias:%22%20%20%20%20Now,%20Vista%20may%20be%20used%20to%20reference%20the%20com.example.real.estate.View%20and%20View%20may%20be%20used%20to%20reference%20android.view.View%20within%20the%20layout%20file.%20Imported%20types%20may%20be%20used%20as%20type%20references%20in%20variables%20and%20expressions:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Note:%20Android%20Studio%20does%20not%20yet%20handle%20imports%20so%20the%20autocomplete%20for%20imported%20variables%20may%20not%20work%20in%20your%20IDE.%20Your%20application%20will%20still%20compile%20fine%20and%20you%20can%20work%20around%20the%20IDE%20issue%20by%20using%20fully%20qualified%20names%20in%20your%20variable%20definitions.%20%20TextView%20%20%20%20android:text=%22@%7B((User)(user.connection)).lastName%7D%22%20%20%20%20android:layout_width=%22wrap_content%22%20%20%20%20android:layout_height=%22wrap_content%22/%20Imported%20types%20may%20also%20be%20used%20when%20referencing%20static%20fields%20and%20methods%20in%20expressions:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%80%A6%20TextView%20%20%20%20android:text=%22@%7BMyStringUtils.capitalize(user.lastName)%7D%22%20%20%20%20android:layout_width=%22wrap_content%22%20%20%20%20android:layout_height=%22wrap_content%22/%20Just%20as%20in%20Java,%20java.lang.%20is%20imported%20automatically.%20%20Variables%20Any%20number%20of%20variable%20elements%20may%20be%20used%20inside%20the%20data%20element.%20Each%20variable%20element%20describes%20a%20property%20that%20may%20be%20set%20on%20the%20layout%20to%20be%20used%20in%20binding%20expressions%20within%20the%20layout%20file.%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20The%20variable%20types%20are%20inspected%20at%20compile%20time,%20so%20if%20a%20variable%20implements%20Observable%20or%20is%20an%20observable%20collection,%20that%20should%20be%20reflected%20in%20the%20type.%20If%20the%20variable%20is%20a%20base%20class%20or%20interface%20that%20does%20not%20implement%20the%20Observable%20interface,%20the%20variables%20will%20not%20be%20observed!%20%20When%20there%20are%20different%20layout%20files%20for%20various%20configurations%20(e.g.%20landscape%20or%20portrait),%20the%20variables%20will%20be%20combined.%20There%20must%20not%20be%20conflicting%20variable%20definitions%20between%20these%20layout%20files.%20%20The%20generated%20binding%20class%20will%20have%20a%20setter%20and%20getter%20for%20each%20of%20the%20described%20variables.%20The%20variables%20will%20take%20the%20default%20Java%20values%20until%20the%20setter%20is%20called%20%E2%80%94%20null%20for%20reference%20types,%200%20for%20int,%20false%20for%20boolean,%20etc.%20%20A%20special%20variable%20named%20context%20is%20generated%20for%20use%20in%20binding%20expressions%20as%20needed.%20The%20value%20for%20context%20is%20the%20Context%20from%20the%20root%20View’s%20getContext().%20The%20context%20variable%20will%20be%20overridden%20by%20an%20explicit%20variable%20declaration%20with%20that%20name.%20%20Custom%20Binding%20Class%20Names%20By%20default,%20a%20Binding%20class%20is%20generated%20based%20on%20the%20name%20of%20the%20layout%20file,%20starting%20it%20with%20upper-case,%20removing%20underscores%20(%20_%20)%20and%20capitalizing%20the%20following%20letter%20and%20then%20suffixing%20%22Binding%22.%20This%20class%20will%20be%20placed%20in%20a%20databinding%20package%20under%20the%20module%20package.%20For%20example,%20the%20layout%20file%20contact_item.xml%20will%20generate%20ContactItemBinding.%20If%20the%20module%20package%20is%20com.example.my.app,%20then%20it%20will%20be%20placed%20in%20com.example.my.app.databinding.%20%20Binding%20classes%20may%20be%20renamed%20or%20placed%20in%20different%20packages%20by%20adjusting%20the%20class%20attribute%20of%20the%20data%20element.%20For%20example:%20%20%20%20%20%20%20…%20%20This%20generates%20the%20binding%20class%20as%20ContactItem%20in%20the%20databinding%20package%20in%20the%20module%20package.%20If%20the%20class%20should%20be%20generated%20in%20a%20different%20package%20within%20the%20module%20package,%20it%20may%20be%20prefixed%20with%20%22.%22:%20%20%20%20%20%20%20…%20%20In%20this%20case,%20ContactItem%20is%20generated%20in%20the%20module%20package%20directly.%20Any%20package%20may%20be%20used%20if%20the%20full%20package%20is%20provided:%20%20%20%20%20%20%20…%20%20Includes%20Variables%20may%20be%20passed%20into%20an%20included%20layout’s%20binding%20from%20the%20containing%20layout%20by%20using%20the%20application%20namespace%20and%20the%20variable%20name%20in%20an%20attribute:%20%20<?xml%20version=%221.0%22%20encoding=%22utf-8%22?>%20layout%20xmlns:android=%22http:/schemas.android.com/apk/res/android%22%20%20%20%20%20%20%20%20%20xmlns:bind=%22http:/schemas.android.com/apk/res-auto%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20include%20layout=%22@layout/name%22%20%20%20%20%20%20%20%20%20%20%20%20bind:user=%22@%7Buser%7D%22/%20%20%20%20%20%20%20%20include%20layout=%22@layout/contact%22%20%20%20%20%20%20%20%20%20%20%20%20bind:user=%22@%7Buser%7D%22/%20%20%20%20%20%20Here,%20there%20must%20be%20a%20user%20variable%20in%20both%20the%20name.xml%20and%20contact.xml%20layout%20files.%20%20Data%20binding%20does%20not%20support%20include%20as%20a%20direct%20child%20of%20a%20merge%20element.%20For%20example,%20the%20following%20layout%20is%20not%20supported:%20%20<?xml%20version=%221.0%22%20encoding=%22utf-8%22?>%20layout%20xmlns:android=%22http:/schemas.android.com/apk/res/android%22%20%20%20%20%20%20%20%20%20xmlns:bind=%22http:/schemas.android.com/apk/res-auto%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20include%20layout=%22@layout/name%22%20%20%20%20%20%20%20%20%20%20%20%20bind:user=%22@%7Buser%7D%22/%20%20%20%20%20%20%20%20include%20layout=%22@layout/contact%22%20%20%20%20%20%20%20%20%20%20%20%20bind:user=%22@%7Buser%7D%22/%20%20%20%20%20%20Expression%20Language%20Common%20Features%20The%20expression%20language%20looks%20a%20lot%20like%20a%20Java%20expression.%20These%20are%20the%20same:%20%20Mathematical%20+%20-%20/%20*%20%%20String%20concatenation%20+%20Logical%20&&%20%7C%7C%20Binary%20&%20%7C%20%5E%20Unary%20+%20-%20!%20~%20Shift%20>>%20>>>%20<<%20Comparison%20==%20>%20<%20>=%20<=%20instanceof%20Grouping%20()%20Literals%20-%20character,%20String,%20numeric,%20null%20Cast%20Method%20calls%20Field%20access%20Array%20access%20%5B%5D%20Ternary%20operator%20?:%20Examples:%20%20android:text=%22@%7BString.valueOf(index%20+%201)%7D%22%20android:visibility=%22@%7Bage%20<%2013%20?%20View.GONE%20:%20View.VISIBLE%7D%22%20android:transitionName=’@%7B%22image_%22%20+%20id%7D’%20Missing%20Operations%20A%20few%20operations%20are%20missing%20from%20the%20expression%20syntax%20that%20you%20can%20use%20in%20Java.%20%20this%20super%20new%20Explicit%20generic%20invocation%20Null%20Coalescing%20Operator%20The%20null%20coalescing%20operator%20(??)%20chooses%20the%20left%20operand%20if%20it%20is%20not%20null%20or%20the%20right%20if%20it%20is%20null.%20%20android:text=%22@%7Buser.displayName%20??%20user.lastName%7D%22%20This%20is%20functionally%20equivalent%20to:%20%20android:text=%22@%7Buser.displayName%20!=%20null%20?%20user.displayName%20:%20user.lastName%7D%22%20Property%20Reference%20The%20first%20was%20already%20discussed%20in%20the%20Writing%20your%20first%20data%20binding%20expressions%20above:%20short%20form%20JavaBean%20references.%20When%20an%20expression%20references%20a%20property%20on%20a%20class,%20it%20uses%20the%20same%20format%20for%20fields,%20getters,%20and%20ObservableFields.%20%20android:text=%22@%7Buser.lastName%7D%22%20Avoiding%20NullPointerException%20Generated%20data%20binding%20code%20automatically%20checks%20for%20nulls%20and%20avoid%20null%20pointer%20exceptions.%20For%20example,%20in%20the%20expression%20@%7Buser.name%7D,%20if%20user%20is%20null,%20user.name%20will%20be%20assigned%20its%20default%20value%20(null).%20If%20you%20were%20referencing%20user.age,%20where%20age%20is%20an%20int,%20then%20it%20would%20default%20to%200.%20%20Collections%20Common%20collections:%20arrays,%20lists,%20sparse%20lists,%20and%20maps,%20may%20be%20accessed%20using%20the%20%5B%5D%20operator%20for%20convenience.%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%80%A6%20android:text=%22@%7Blist%5Bindex%5D%7D%22%20%E2%80%A6%20android:text=%22@%7Bsparse%5Bindex%5D%7D%22%20%E2%80%A6%20android:text=%22@%7Bmap%5Bkey%5D%7D%22%20String%20Literals%20When%20using%20single%20quotes%20around%20the%20attribute%20value,%20it%20is%20easy%20to%20use%20double%20quotes%20in%20the%20expression:%20%20android:text=’@%7Bmap%5B%22firstName%22%5D%7D’%20It%20is%20also%20possible%20to%20use%20double%20quotes%20to%20surround%20the%20attribute%20value.%20When%20doing%20so,%20String%20literals%20should%20either%20use%20the%20’%20or%20back%20quote%20(%60).%20%20android:text=%22@%7Bmap%5B%60firstName%60%7D%22%20android:text=%22@%7Bmap%5B’firstName’%5D%7D%22%20Resources%20It%20is%20possible%20to%20access%20resources%20as%20part%20of%20expressions%20using%20the%20normal%20syntax:%20%20android:padding=%22@%7Blarge?%20@dimen/largePadding%20:%20@dimen/smallPadding%7D%22%20Format%20strings%20and%20plurals%20may%20be%20evaluated%20by%20providing%20parameters:%20%20android:text=%22@%7B@string/nameFormat(firstName,%20lastName)%7D%22%20android:text=%22@%7B@plurals/banana(bananaCount)%7D%22%20When%20a%20plural%20takes%20multiple%20parameters,%20all%20parameters%20should%20be%20passed:%20%20%20%20%20Have%20an%20orange%20%20%20Have%20%d%20oranges%20%20android:text=%22@%7B@plurals/orange(orangeCount,%20orangeCount)%7D%22%20Some%20resources%20require%20explicit%20type%20evaluation.%20%20Type%09Normal%20Reference%09Expression%20Reference%20String%5B%5D%09@array%09@stringArray%20int%5B%5D%09@array%09@intArray%20TypedArray%09@array%09@typedArray%20Animator%09@animator%09@animator%20StateListAnimator%09@animator%09@stateListAnimator%20color%20int%09@color%09@color%20ColorStateList%09@color%09@colorStateList%20Data%20Objects%20Any%20plain%20old%20Java%20object%20(POJO)%20may%20be%20used%20for%20data%20binding,%20but%20modifying%20a%20POJO%20will%20not%20cause%20the%20UI%20to%20update.%20The%20real%20power%20of%20data%20binding%20can%20be%20used%20by%20giving%20your%20data%20objects%20the%20ability%20to%20notify%20when%20data%20changes.%20There%20are%20three%20different%20data%20change%20notification%20mechanisms,%20Observable%20objects,%20observable%20fields,%20and%20observable%20collections.%20%20When%20one%20of%20these%20observable%20data%20object%20is%20bound%20to%20the%20UI%20and%20a%20property%20of%20the%20data%20object%20changes,%20the%20UI%20will%20be%20updated%20automatically.%20%20Observable%20Objects%20A%20class%20implementing%20the%20Observable%20interface%20will%20allow%20the%20binding%20to%20attach%20a%20single%20listener%20to%20a%20bound%20object%20to%20listen%20for%20changes%20of%20all%20properties%20on%20that%20object.%20%20The%20Observable%20interface%20has%20a%20mechanism%20to%20add%20and%20remove%20listeners,%20but%20notifying%20is%20up%20to%20the%20developer.%20To%20make%20development%20easier,%20a%20base%20class,%20BaseObservable,%20was%20created%20to%20implement%20the%20listener%20registration%20mechanism.%20The%20data%20class%20implementer%20is%20still%20responsible%20for%20notifying%20when%20the%20properties%20change.%20This%20is%20done%20by%20assigning%20a%20Bindable%20annotation%20to%20the%20getter%20and%20notifying%20in%20the%20setter.%20%20private%20static%20class%20User%20extends%20BaseObservable%20%7B%20%20%20%20private%20String%20firstName;%20%20%20%20private%20String%20lastName;%20%20%20%20@Bindable%20%20%20%20public%20String%20getFirstName()%20%7B%20%20%20%20%20%20%20%20return%20this.firstName;%20%20%20%20%7D%20%20%20%20@Bindable%20%20%20%20public%20String%20getLastName()%20%7B%20%20%20%20%20%20%20%20return%20this.lastName;%20%20%20%20%7D%20%20%20%20public%20void%20setFirstName(String%20firstName)%20%7B%20%20%20%20%20%20%20%20this.firstName%20=%20firstName;%20%20%20%20%20%20%20%20notifyPropertyChanged(BR.firstName);%20%20%20%20%7D%20%20%20%20public%20void%20setLastName(String%20lastName)%20%7B%20%20%20%20%20%20%20%20this.lastName%20=%20lastName;%20%20%20%20%20%20%20%20notifyPropertyChanged(BR.lastName);%20%20%20%20%7D%20%7D%20The%20Bindable%20annotation%20generates%20an%20entry%20in%20the%20BR%20class%20file%20during%20compilation.%20The%20BR%20class%20file%20will%20be%20generated%20in%20the%20module%20package.%20If%20the%20base%20class%20for%20data%20classes%20cannot%20be%20changed,%20the%20Observable%20interface%20may%20be%20implemented%20using%20the%20convenient%20PropertyChangeRegistry%20to%20store%20and%20notify%20listeners%20efficiently.%20%20ObservableFields%20A%20little%20work%20is%20involved%20in%20creating%20Observable%20classes,%20so%20developers%20who%20want%20to%20save%20time%20or%20have%20few%20properties%20may%20use%20ObservableField%20and%20its%20siblings%20ObservableBoolean,%20ObservableByte,%20ObservableChar,%20ObservableShort,%20ObservableInt,%20ObservableLong,%20ObservableFloat,%20ObservableDouble,%20and%20ObservableParcelable.%20ObservableFields%20are%20self-contained%20observable%20objects%20that%20have%20a%20single%20field.%20The%20primitive%20versions%20avoid%20boxing%20and%20unboxing%20during%20access%20operations.%20To%20use,%20create%20a%20public%20final%20field%20in%20the%20data%20class:%20%20private%20static%20class%20User%20%7B%20%20%20%20public%20final%20ObservableField%20firstName%20=%20%20%20%20%20%20%20%20new%20ObservableField<>();%20%20%20%20public%20final%20ObservableField%20lastName%20=%20%20%20%20%20%20%20%20new%20ObservableField<>();%20%20%20%20public%20final%20ObservableInt%20age%20=%20new%20ObservableInt();%20%7D%20That’s%20it!%20To%20access%20the%20value,%20use%20the%20set%20and%20get%20accessor%20methods:%20%20user.firstName.set(%22Google%22);%20int%20age%20=%20user.age.get();%20Observable%20Collections%20Some%20applications%20use%20more%20dynamic%20structures%20to%20hold%20data.%20Observable%20collections%20allow%20keyed%20access%20to%20these%20data%20objects.%20ObservableArrayMap%20is%20useful%20when%20the%20key%20is%20a%20reference%20type,%20such%20as%20String.%20%20ObservableArrayMap%20user%20=%20new%20ObservableArrayMap<>();%20user.put(%22firstName%22,%20%22Google%22);%20user.put(%22lastName%22,%20%22Inc.%22);%20user.put(%22age%22,%2017);%20In%20the%20layout,%20the%20map%20may%20be%20accessed%20through%20the%20String%20keys:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%80%A6%20TextView%20%20%20%20android:text='@%7Buser%5B%22lastName%22%5D%7D'%20%20%20%20android:layout_width=%22wrap_content%22%20%20%20%20android:layout_height=%22wrap_content%22/%20TextView%20%20%20%20android:text='@%7BString.valueOf(1%20+%20(Integer)user%5B%22age%22%5D)%7D'%20%20%20%20android:layout_width=%22wrap_content%22%20%20%20%20android:layout_height=%22wrap_content%22/%20ObservableArrayList%20is%20useful%20when%20the%20key%20is%20an%20integer:%20%20ObservableArrayList%20user%20=%20new%20ObservableArrayList<>();%20user.add(%22Google%22);%20user.add(%22Inc.%22);%20user.add(17);%20In%20the%20layout,%20the%20list%20may%20be%20accessed%20through%20the%20indices:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%80%A6%20TextView%20%20%20%20android:text='@%7Buser%5BFields.LAST_NAME%5D%7D'%20%20%20%20android:layout_width=%22wrap_content%22%20%20%20%20android:layout_height=%22wrap_content%22/%20TextView%20%20%20%20android:text='@%7BString.valueOf(1%20+%20(Integer)user%5BFields.AGE%5D)%7D'%20%20%20%20android:layout_width=%22wrap_content%22%20%20%20%20android:layout_height=%22wrap_content%22/%20Generated%20Binding%20The%20generated%20binding%20class%20links%20the%20layout%20variables%20with%20the%20Views%20within%20the%20layout.%20As%20discussed%20earlier,%20the%20name%20and%20package%20of%20the%20Binding%20may%20be%20customized.%20The%20Generated%20binding%20classes%20all%20extend%20ViewDataBinding.%20%20Creating%20The%20binding%20should%20be%20created%20soon%20after%20inflation%20to%20ensure%20that%20the%20View%20hierarchy%20is%20not%20disturbed%20prior%20to%20binding%20to%20the%20Views%20with%20expressions%20within%20the%20layout.%20There%20are%20a%20few%20ways%20to%20bind%20to%20a%20layout.%20The%20most%20common%20is%20to%20use%20the%20static%20methods%20on%20the%20Binding%20class.The%20inflate%20method%20inflates%20the%20View%20hierarchy%20and%20binds%20to%20it%20all%20it%20one%20step.%20There%20is%20a%20simpler%20version%20that%20only%20takes%20a%20LayoutInflater%20and%20one%20that%20takes%20a%20ViewGroup%20as%20well:%20%20MyLayoutBinding%20binding%20=%20MyLayoutBinding.inflate(layoutInflater);%20MyLayoutBinding%20binding%20=%20MyLayoutBinding.inflate(layoutInflater,%20viewGroup,%20false);%20If%20the%20layout%20was%20inflated%20using%20a%20different%20mechanism,%20it%20may%20be%20bound%20separately:%20%20MyLayoutBinding%20binding%20=%20MyLayoutBinding.bind(viewRoot);%20Sometimes%20the%20binding%20cannot%20be%20known%20in%20advance.%20In%20such%20cases,%20the%20binding%20can%20be%20created%20using%20the%20DataBindingUtil%20class:%20%20ViewDataBinding%20binding%20=%20DataBindingUtil.inflate(LayoutInflater,%20layoutId,%20%20%20%20%20parent,%20attachToParent);%20ViewDataBinding%20binding%20=%20DataBindingUtil.bindTo(viewRoot,%20layoutId);%20Views%20With%20IDs%20A%20public%20final%20field%20will%20be%20generated%20for%20each%20View%20with%20an%20ID%20in%20the%20layout.%20The%20binding%20does%20a%20single%20pass%20on%20the%20View%20hierarchy,%20extracting%20the%20Views%20with%20IDs.%20This%20mechanism%20can%20be%20faster%20than%20calling%20findViewById%20for%20several%20Views.%20For%20example:%20%20layout%20xmlns:android=%22http:/schemas.android.com/apk/res/android%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20TextView%20android:layout_width=%22wrap_content%22%20%20%20%20%20%20%20%20%20%20%20%20android:layout_height=%22wrap_content%22%20%20%20%20%20%20%20%20%20%20%20%20android:text=%22@%7Buser.firstName%7D%22%20%20%20%20android:id=%22@+id/firstName%22/%20%20%20%20%20%20%20%20TextView%20android:layout_width=%22wrap_content%22%20%20%20%20%20%20%20%20%20%20%20%20android:layout_height=%22wrap_content%22%20%20%20%20%20%20%20%20%20%20%20%20android:text=%22@%7Buser.lastName%7D%22%20%20%20android:id=%22@+id/lastName%22/%20%20%20%20%20%20Will%20generate%20a%20binding%20class%20with:%20%20public%20final%20TextView%20firstName;%20public%20final%20TextView%20lastName;%20IDs%20are%20not%20nearly%20as%20necessary%20as%20without%20data%20binding,%20but%20there%20are%20still%20some%20instances%20where%20access%20to%20Views%20are%20still%20necessary%20from%20code.%20%20Variables%20Each%20variable%20will%20be%20given%20accessor%20methods.%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20will%20generate%20setters%20and%20getters%20in%20the%20binding:%20%20public%20abstract%20com.example.User%20getUser();%20public%20abstract%20void%20setUser(com.example.User%20user);%20public%20abstract%20Drawable%20getImage();%20public%20abstract%20void%20setImage(Drawable%20image);%20public%20abstract%20String%20getNote();%20public%20abstract%20void%20setNote(String%20note);%20ViewStubs%20ViewStubs%20are%20a%20little%20different%20from%20normal%20Views.%20They%20start%20off%20invisible%20and%20when%20they%20either%20are%20made%20visible%20or%20are%20explicitly%20told%20to%20inflate,%20they%20replace%20themselves%20in%20the%20layout%20by%20inflating%20another%20layout.%20%20Because%20the%20ViewStub%20essentially%20disappears%20from%20the%20View%20hierarchy,%20the%20View%20in%20the%20binding%20object%20must%20also%20disappear%20to%20allow%20collection.%20Because%20the%20Views%20are%20final,%20a%20ViewStubProxy%20object%20takes%20the%20place%20of%20the%20ViewStub,%20giving%20the%20developer%20access%20to%20the%20ViewStub%20when%20it%20exists%20and%20also%20access%20to%20the%20inflated%20View%20hierarchy%20when%20the%20ViewStub%20has%20been%20inflated.%20%20When%20inflating%20another%20layout,%20a%20binding%20must%20be%20established%20for%20the%20new%20layout.%20Therefore,%20the%20ViewStubProxy%20must%20listen%20to%20the%20ViewStub’s%20ViewStub.OnInflateListener%20and%20establish%20the%20binding%20at%20that%20time.%20Since%20only%20one%20can%20exist,%20the%20ViewStubProxy%20allows%20the%20developer%20to%20set%20an%20OnInflateListener%20on%20it%20that%20it%20will%20call%20after%20establishing%20the%20binding.%20%20Advanced%20Binding%20Dynamic%20Variables%20At%20times,%20the%20specific%20binding%20class%20won’t%20be%20known.%20For%20example,%20a%20RecyclerView.Adapter%20operating%20against%20arbitrary%20layouts%20won’t%20know%20the%20specific%20binding%20class.%20It%20still%20must%20assign%20the%20binding%20value%20during%20the%20onBindViewHolder(VH,%20int).%20%20In%20this%20example,%20all%20layouts%20that%20the%20RecyclerView%20binds%20to%20have%20an%20%22item%22%20variable.%20The%20BindingHolder%20has%20a%20getBinding%20method%20returning%20the%20ViewDataBinding%20base.%20%20public%20void%20onBindViewHolder(BindingHolder%20holder,%20int%20position)%20%7B%20%20%20%20final%20T%20item%20=%20mItems.get(position);%20%20%20%20holder.getBinding().setVariable(BR.item,%20item);%20%20%20%20holder.getBinding().executePendingBindings();%20%7D%20Immediate%20Binding%20When%20a%20variable%20or%20observable%20changes,%20the%20binding%20will%20be%20scheduled%20to%20change%20before%20the%20next%20frame.%20There%20are%20times,%20however,%20when%20binding%20must%20be%20executed%20immediately.%20To%20force%20execution,%20use%20the%20executePendingBindings()%20method.%20%20Background%20Thread%20You%20can%20change%20your%20data%20model%20in%20a%20background%20thread%20as%20long%20as%20it%20is%20not%20a%20collection.%20Data%20binding%20will%20localize%20each%20variable%20/%20field%20while%20evaluating%20to%20avoid%20any%20concurrency%20issues.%20%20Attribute%20Setters%20Whenever%20a%20bound%20value%20changes,%20the%20generated%20binding%20class%20must%20call%20a%20setter%20method%20on%20the%20View%20with%20the%20binding%20expression.%20The%20data%20binding%20framework%20has%20ways%20to%20customize%20which%20method%20to%20call%20to%20set%20the%20value.%20%20Automatic%20Setters%20For%20an%20attribute,%20data%20binding%20tries%20to%20find%20the%20method%20setAttribute.%20The%20namespace%20for%20the%20attribute%20does%20not%20matter,%20only%20the%20attribute%20name%20itself.%20For%20example,%20an%20expression%20associated%20with%20TextView’s%20attribute%20android:text%20will%20look%20for%20a%20setText(String).%20If%20the%20expression%20returns%20an%20int,%20data%20binding%20will%20search%20for%20a%20setText(int)%20method.%20Be%20careful%20to%20have%20the%20expression%20return%20the%20correct%20type,%20casting%20if%20necessary.%20Note%20that%20data%20binding%20will%20work%20even%20if%20no%20attribute%20exists%20with%20the%20given%20name.%20You%20can%20then%20easily%20%22create%22%20attributes%20for%20any%20setter%20by%20using%20data%20binding.%20For%20example,%20support%20DrawerLayout%20doesn’t%20have%20any%20attributes,%20but%20plenty%20of%20setters.%20You%20can%20use%20the%20automatic%20setters%20to%20use%20one%20of%20these.%20%20android.support.v4.widget.DrawerLayout%20%20%20%20%20android:layout_width=%22wrap_content%22%20%20%20%20%20android:layout_height=%22wrap_content%22%20%20%20%20%20app:scrimColor=%22@%7B@color/scrim%7D%22%20%20%20%20%20app:drawerListener=%22@%7Bfragment.drawerListener%7D%22/%20Renamed%20Setters%20Some%20attributes%20have%20setters%20that%20don’t%20match%20by%20name.%20For%20these%20methods,%20an%20attribute%20may%20be%20associated%20with%20the%20setter%20through%20BindingMethods%20annotation.%20This%20must%20be%20associated%20with%20a%20class%20and%20contains%20BindingMethod%20annotations,%20one%20for%20each%20renamed%20method.%20For%20example,%20the%20android:tint%20attribute%20is%20really%20associated%20with%20setImageTintList(ColorStateList),%20not%20setTint.%20%20@BindingMethods(%7B%20%20%20%20%20%20%20%20@BindingMethod(type%20=%20%22android.widget.ImageView%22,%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20attribute%20=%20%22android:tint%22,%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20method%20=%20%22setImageTintList%22),%20%7D)%20It%20is%20unlikely%20that%20developers%20will%20need%20to%20rename%20setters;%20the%20android%20framework%20attributes%20have%20already%20been%20implemented.%20%20Custom%20Setters%20Some%20attributes%20need%20custom%20binding%20logic.%20For%20example,%20there%20is%20no%20associated%20setter%20for%20the%20android:paddingLeft%20attribute.%20Instead,%20setPadding(left,%20top,%20right,%20bottom)%20exists.%20A%20static%20binding%20adapter%20method%20with%20the%20BindingAdapter%20annotation%20allows%20the%20developer%20to%20customize%20how%20a%20setter%20for%20an%20attribute%20is%20called.%20%20The%20android%20attributes%20have%20already%20had%20BindingAdapters%20created.%20For%20example,%20here%20is%20the%20one%20for%20paddingLeft:%20%20@BindingAdapter(%22android:paddingLeft%22)%20public%20static%20void%20setPaddingLeft(View%20view,%20int%20padding)%20%7B%20%20%20%20view.setPadding(padding,%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20view.getPaddingTop(),%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20view.getPaddingRight(),%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20view.getPaddingBottom());%20%7D%20Binding%20adapters%20are%20useful%20for%20other%20types%20of%20customization.%20For%20example,%20a%20custom%20loader%20can%20be%20called%20off-thread%20to%20load%20an%20image.%20%20Developer-created%20binding%20adapters%20will%20override%20the%20data%20binding%20default%20adapters%20when%20there%20is%20a%20conflict.%20%20You%20can%20also%20have%20adapters%20that%20receive%20multiple%20parameters.%20%20@BindingAdapter(%7B%22bind:imageUrl%22,%20%22bind:error%22%7D)%20public%20static%20void%20loadImage(ImageView%20view,%20String%20url,%20Drawable%20error)%20%7B%20%20%20%20Picasso.with(view.getContext()).load(url).error(error).into(view);%20%7D%20ImageView%20app:imageUrl=%22@%7Bvenue.imageUrl%7D%22%20app:error=%22@%7B@drawable/venueError%7D%22/%20This%20adapter%20will%20be%20called%20if%20both%20imageUrl%20and%20error%20are%20used%20for%20an%20ImageView%20and%20imageUrl%20is%20a%20string%20and%20error%20is%20a%20drawable.%20%20Custom%20namespaces%20are%20ignored%20during%20matching.%20You%20can%20also%20write%20adapters%20for%20android%20namespace.%20Binding%20adapter%20methods%20may%20optionally%20take%20the%20old%20values%20in%20their%20handlers.%20A%20method%20taking%20old%20and%20new%20values%20should%20have%20all%20old%20values%20for%20the%20attributes%20come%20first,%20followed%20by%20the%20new%20values:%20%20@BindingAdapter(%22android:paddingLeft%22)%20public%20static%20void%20setPaddingLeft(View%20view,%20int%20oldPadding,%20int%20newPadding)%20%7B%20%20%20%20if%20(oldPadding%20!=%20newPadding)%20%7B%20%20%20%20%20%20%20%20view.setPadding(newPadding,%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20view.getPaddingTop(),%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20view.getPaddingRight(),%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20view.getPaddingBottom());%20%20%20%20%7D%20%7D%20Event%20handlers%20may%20only%20be%20used%20with%20interfaces%20or%20abstract%20classes%20with%20one%20abstract%20method.%20For%20example:%20%20@BindingAdapter(%22android:onLayoutChange%22)%20public%20static%20void%20setOnLayoutChangeListener(View%20view,%20View.OnLayoutChangeListener%20oldValue,%20%20%20%20%20%20%20%20View.OnLayoutChangeListener%20newValue)%20%7B%20%20%20%20%20if%20(Build.VERSION.SDK_INT%20>=%20Build.VERSION_CODES.HONEYCOMB)%20%7B%20%20%20%20%20%20%20%20%20if%20(oldValue%20!=%20null)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20view.removeOnLayoutChangeListener(oldValue);%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20if%20(newValue%20!=%20null)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20view.addOnLayoutChangeListener(newValue);%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%7D%20%7D%20When%20a%20listener%20has%20multiple%20methods,%20it%20must%20be%20split%20into%20multiple%20listeners.%20For%20example,%20View.OnAttachStateChangeListener%20has%20two%20methods:%20onViewAttachedToWindow()%20and%20onViewDetachedFromWindow().%20We%20must%20then%20create%20two%20interfaces%20to%20differentiate%20the%20attributes%20and%20handlers%20for%20them.%20%20@TargetApi(VERSION_CODES.HONEYCOMB_MR1)%20public%20interface%20OnViewDetachedFromWindow%20%7B%20%20%20%20%20void%20onViewDetachedFromWindow(View%20v);%20%7D%20%20@TargetApi(VERSION_CODES.HONEYCOMB_MR1)%20public%20interface%20OnViewAttachedToWindow%20%7B%20%20%20%20%20void%20onViewAttachedToWindow(View%20v);%20%7D%20Because%20changing%20one%20listener%20will%20also%20affect%20the%20other,%20we%20must%20have%20three%20different%20binding%20adapters,%20one%20for%20each%20attribute%20and%20one%20for%20both,%20should%20they%20both%20be%20set.%20%20@BindingAdapter(%22android:onViewAttachedToWindow%22)%20public%20static%20void%20setListener(View%20view,%20OnViewAttachedToWindow%20attached)%20%7B%20%20%20%20%20setListener(view,%20null,%20attached);%20%7D%20%20@BindingAdapter(%22android:onViewDetachedFromWindow%22)%20public%20static%20void%20setListener(View%20view,%20OnViewDetachedFromWindow%20detached)%20%7B%20%20%20%20%20setListener(view,%20detached,%20null);%20%7D%20%20@BindingAdapter(%7B%22android:onViewDetachedFromWindow%22,%20%22android:onViewAttachedToWindow%22%7D)%20public%20static%20void%20setListener(View%20view,%20final%20OnViewDetachedFromWindow%20detach,%20%20%20%20%20%20%20%20%20final%20OnViewAttachedToWindow%20attach)%20%7B%20%20%20%20%20if%20(VERSION.SDK_INT%20>=%20VERSION_CODES.HONEYCOMB_MR1)%20%7B%20%20%20%20%20%20%20%20%20final%20OnAttachStateChangeListener%20newListener;%20%20%20%20%20%20%20%20%20if%20(detach%20==%20null%20&&%20attach%20==%20null)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20newListener%20=%20null;%20%20%20%20%20%20%20%20%20%7D%20else%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20newListener%20=%20new%20OnAttachStateChangeListener()%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20@Override%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20public%20void%20onViewAttachedToWindow(View%20v)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(attach%20!=%20null)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20attach.onViewAttachedToWindow(v);%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20@Override%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20public%20void%20onViewDetachedFromWindow(View%20v)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(detach%20!=%20null)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20detach.onViewDetachedFromWindow(v);%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%7D;%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20final%20OnAttachStateChangeListener%20oldListener%20=%20ListenerUtil.trackListener(view,%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20newListener,%20R.id.onAttachStateChangeListener);%20%20%20%20%20%20%20%20%20if%20(oldListener%20!=%20null)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20view.removeOnAttachStateChangeListener(oldListener);%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20if%20(newListener%20!=%20null)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20view.addOnAttachStateChangeListener(newListener);%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%7D%20%7D%20The%20above%20example%20is%20slightly%20more%20complicated%20than%20normal%20because%20View%20uses%20add%20and%20remove%20for%20the%20listener%20instead%20of%20a%20set%20method%20for%20View.OnAttachStateChangeListener.%20The%20android.databinding.adapters.ListenerUtil%20class%20helps%20keep%20track%20of%20the%20previous%20listeners%20so%20that%20they%20may%20be%20removed%20in%20the%20Binding%20Adaper.%20%20By%20annotating%20the%20interfaces%20OnViewDetachedFromWindow%20and%20OnViewAttachedToWindow%20with%20@TargetApi(VERSION_CODES.HONEYCOMB_MR1),%20the%20data%20binding%20code%20generator%20knows%20that%20the%20listener%20should%20only%20be%20generated%20when%20running%20on%20Honeycomb%20MR1%20and%20new%20devices,%20the%20same%20version%20supported%20by%20addOnAttachStateChangeListener(View.OnAttachStateChangeListener).%20%20Converters%20Object%20Conversions%20When%20an%20Object%20is%20returned%20from%20a%20binding%20expression,%20a%20setter%20will%20be%20chosen%20from%20the%20automatic,%20renamed,%20and%20custom%20setters.%20The%20Object%20will%20be%20cast%20to%20a%20parameter%20type%20of%20the%20chosen%20setter.%20%20This%20is%20a%20convenience%20for%20those%20using%20ObservableMaps%20to%20hold%20data.%20for%20example:%20%20TextView%20%20%20%20android:text='@%7BuserMap%5B%22lastName%22%5D%7D'%20%20%20%20android:layout_width=%22wrap_content%22%20%20%20%20android:layout_height=%22wrap_content%22/%20The%20userMap%20returns%20an%20Object%20and%20that%20Object%20will%20be%20automatically%20cast%20to%20parameter%20type%20found%20in%20the%20setter%20setText(CharSequence).%20When%20there%20may%20be%20confusion%20about%20the%20parameter%20type,%20the%20developer%20will%20need%20to%20cast%20in%20the%20expression.%20%20Custom%20Conversions%20Sometimes%20conversions%20should%20be%20automatic%20between%20specific%20types.%20For%20example,%20when%20setting%20the%20background:%20%20View%20%20%20%20android:background=%22@%7BisError%20?%20@color/red%20:%20@color/white%7D%22%20%20%20%20android:layout_width=%22wrap_content%22%20%20%20%20android:layout_height=%22wrap_content%22/%20Here,%20the%20background%20takes%20a%20Drawable,%20but%20the%20color%20is%20an%20integer.%20Whenever%20a%20Drawable%20is%20expected%20and%20an%20integer%20is%20returned,%20the%20int%20should%20be%20converted%20to%20a%20ColorDrawable.%20This%20conversion%20is%20done%20using%20a%20static%20method%20with%20a%20BindingConversion%20annotation:%20%20@BindingConversion%20public%20static%20ColorDrawable%20convertColorToDrawable(int%20color)%20%7B%20%20%20%20return%20new%20ColorDrawable(color);%20%7D%20Note%20that%20conversions%20only%20happen%20at%20the%20setter%20level,%20so%20it%20is%20not%20allowed%20to%20mix%20types%20like%20this:%20%20View%20%20%20%20android:background=%22@%7BisError%20?%20@drawable/error%20:%20@color/white%7D%22%20%20%20%20android:layout_width=%22wrap_content%22%20%20%20%20android:layout_height=%22wrap_content%22/%20Android%20Studio%20Support%20for%20Data%20Binding%20Android%20Studio%20supports%20many%20of%20the%20code%20editing%20features%20for%20data%20binding%20code.%20For%20example,%20it%20supports%20the%20following%20features%20for%20data%20binding%20expressions:%20%20Syntax%20highlighting%20Flagging%20of%20expression%20language%20syntax%20errors%20XML%20code%20completion%20References,%20including%20navigation%20(such%20as%20navigate%20to%20a%20declaration)%20and%20quick%20documentation%20Note:%20Arrays%20and%20a%20generic%20type,%20such%20as%20the%20Observable%20class,%20might%20display%20errors%20when%20there%20are%20no%20errors.%20%20The%20Preview%20pane%20displays%20default%20values%20for%20data%20binding%20expressions%20if%20provided.%20In%20the%20following%20example%20excerpt%20of%20an%20element%20from%20a%20layout%20XML%20file,%20the%20Preview%20pane%20displays%20the%20PLACEHOLDER%20default%20text%20value%20in%20the%20TextView.%20%20TextView%20android:layout_width=%22wrap_content%22%20%20%20%20android:layout_height=%22wrap_content%22%20%20%20%20android:text=%22@%7Buser.firstName,%20default=PLACEHOLDER%7D%22/%20If%20you%20need%20to%20display%20a%20default%20value%20during%20the%20design%20phase%20of%20your%20project,%20you%20can%20also%20use%20tools%20attributes%20instead%20of%20default%20expression%20values,%20as%20described%20in%20Designtime%20Layout%20Attributes.”>https://developer.android.com/topic/libraries/data-binding/index.html>


Data Binding Library

In this document:

                1. This document explains how to use the Data Binding Library to write declarative layouts and minimize the glue code necessary to bind your application logic and layouts.


                  The Data Binding Library offers both flexibility and broad compatibility — it’s a support library, so you can use it with all Android platform versions back to Android 2.1 (API level 7+).


                  To use data binding, Android Plugin for Gradle 1.5.0-alpha1 or higher is required. See how to .


                  Build Environment




                  To get started with Data Binding, download the library from the Support repository in the Android SDK manager.


                  To configure your app to use data binding, add the dataBindingelement to your build.gradle file in the app module.


                  Use the following code snippet to configure data binding:


                  1
                  2
                  3
                  4
                  5
                  6
                  <span style="color: #000000;" class="pln">android </span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                      </span><span style="color: #666600;" class="pun">....</span><span style="color: #000000;" class="pln">
                      dataBinding </span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                          enabled </span><span style="color: #666600;" class="pun">=</span><span style="color: #000088;" class="kwd">true</span><span style="color: #000000;" class="pln">
                      </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                  </span><span style="color: #666600;" class="pun">}</span>


                  If you have an app module that depends on a library which uses data binding, your app module must configure data binding in its build.gradle file as well.


                  Also, make sure you are using a compatible version of Android Studio.Android Studio 1.3 and later provides support for data binding as described in .


                  Data Binding Layout Files




                  Writing your first set of data binding expressions


                  Data-binding layout files are slightly different and start with a root tag of layout followed by a data element and aview root element. This view element is what your root would be in a non-binding layout file. A sample file looks like this:


                  1
                  2
                  3
                  4
                  5
                  6
                  7
                  8
                  9
                  10
                  11
                  12
                  13
                  14
                  15
                  16
                  17
                  <span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"1.0"</span><span style="color: #000000;" class="pln"> encoding</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"utf-8"</span><span style="color: #666600;" class="pun">?></span><span style="color: #000000;" class="pln">
                  </span><span style="color: #000088;" class="tag"><layout</span><span style="color: #882288;" class="atn">xmlns:android</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"http://schemas.android.com/apk/res/android"</span><span style="color: #000088;" class="tag">></span><span style="color: #000000;" class="pln">
                     </span><span style="color: #000088;" class="tag"><data></span><span style="color: #000000;" class="pln">
                         </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"user"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.example.User"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                     </span><span style="color: #000088;" class="tag"></data></span><span style="color: #000000;" class="pln">
                     </span><span style="color: #000088;" class="tag"><LinearLayout</span><span style="color: #000000;" class="pln">
                         </span><span style="color: #882288;" class="atn">android:orientation</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"vertical"</span><span style="color: #000000;" class="pln">
                         </span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"match_parent"</span><span style="color: #000000;" class="pln">
                         </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"match_parent"</span><span style="color: #000088;" class="tag">></span><span style="color: #000000;" class="pln">
                         </span><span style="color: #000088;" class="tag"><TextView</span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                             </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                             </span><span style="color: #882288;" class="atn">android:text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{user.firstName}"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                         </span><span style="color: #000088;" class="tag"><TextView</span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                             </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                             </span><span style="color: #882288;" class="atn">android:text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{user.lastName}"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                     </span><span style="color: #000088;" class="tag"></LinearLayout></span><span style="color: #000000;" class="pln">
                  </span><span style="color: #000088;" class="tag"></layout></span>


                  The user variable within data describes a property that may be used within this layout.


                  1
                  <span style="color: #000088;" class="tag"><</span><strong style="color: inherit;"><span style="color: #000088;" class="tag">variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"user"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.example.User"</span>**<span style="color: #000088;" class="tag">/></span>


                  Expressions within the layout are written in the attribute properties using the “@{}“ syntax. Here, the TextView’s text is set to the firstName property of user:


                  1
                  2
                  3
                  <span style="color: #000088;" class="tag"><TextView</span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                            </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                            </span><span style="color: #882288;" class="atn">android:text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{user.firstName}"</span><span style="color: #000088;" class="tag">/></span>


                  Data Object


                  Let’s assume for now that you have a plain-old Java object (POJO) for User:


                  1
                  2
                  3
                  4
                  5
                  6
                  7
                  8
                  <span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">class</span><span style="color: #660066;" class="typ">User</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                     </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">final</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> firstName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                     </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">final</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> lastName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                     </span><span style="color: #000088;" class="kwd">public</span><span style="color: #660066;" class="typ">User</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> firstName</span><span style="color: #666600;" class="pun">,</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> lastName</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                         </span><span style="color: #000088;" class="kwd">this</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">firstName </span><span style="color: #666600;" class="pun">=</span><span style="color: #000000;" class="pln"> firstName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                         </span><span style="color: #000088;" class="kwd">this</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">lastName </span><span style="color: #666600;" class="pun">=</span><span style="color: #000000;" class="pln"> lastName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                     </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                  </span><span style="color: #666600;" class="pun">}</span>


                  This type of object has data that never changes. It is common in applications to have data that is read once and never changes thereafter. It is also possible to use a JavaBeans objects:


                  1
                  2
                  3
                  4
                  5
                  6
                  7
                  8
                  9
                  10
                  11
                  12
                  13
                  14
                  <span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">class</span><span style="color: #660066;" class="typ">User</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                     </span><span style="color: #000088;" class="kwd">private</span><span style="color: #000088;" class="kwd">final</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> firstName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                     </span><span style="color: #000088;" class="kwd">private</span><span style="color: #000088;" class="kwd">final</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> lastName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                     </span><span style="color: #000088;" class="kwd">public</span><span style="color: #660066;" class="typ">User</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> firstName</span><span style="color: #666600;" class="pun">,</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> lastName</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                         </span><span style="color: #000088;" class="kwd">this</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">firstName </span><span style="color: #666600;" class="pun">=</span><span style="color: #000000;" class="pln"> firstName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                         </span><span style="color: #000088;" class="kwd">this</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">lastName </span><span style="color: #666600;" class="pun">=</span><span style="color: #000000;" class="pln"> lastName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                     </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                     </span><span style="color: #000088;" class="kwd">public</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> getFirstName</span><span style="color: #666600;" class="pun">()</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                         </span><span style="color: #000088;" class="kwd">return</span><span style="color: #000088;" class="kwd">this</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">firstName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                     </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                     </span><span style="color: #000088;" class="kwd">public</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> getLastName</span><span style="color: #666600;" class="pun">()</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                         </span><span style="color: #000088;" class="kwd">return</span><span style="color: #000088;" class="kwd">this</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">lastName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                     </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                  </span><span style="color: #666600;" class="pun">}</span>


                  From the perspective of data binding, these two classes are equivalent. The expression @{user.firstName} used for the TextView’s android:text attribute will access the firstName field in the former class and the getFirstName() method in the latter class. Alternatively, it will also be resolved to firstName() if that method exists.


                  Binding Data


                  By default, a Binding class will be generated based on the name of the layout file, converting it to Pascal case and suffixing “Binding” to it. The above layout file was mainactivity.xml so the generate class wasMainActivityBinding. This class holds all the bindings from the layout properties (e.g. the user variable) to the layout’s Views and knows how to assign values for the binding expressions.The easiest means for creating the bindings is to do it while inflating:


                  1
                  2
                  3
                  4
                  5
                  6
                  7
                  <span style="color: #006666;" class="lit">@Override</span><span style="color: #000000;" class="pln">
                  </span><span style="color: #000088;" class="kwd">protected</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> onCreate</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">Bundle</span><span style="color: #000000;" class="pln"> savedInstanceState</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                     </span><span style="color: #000088;" class="kwd">super</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">onCreate</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">savedInstanceState</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                     </span><span style="color: #660066;" class="typ">MainActivityBinding</span><span style="color: #000000;" class="pln"> binding </span><span style="color: #666600;" class="pun">=</span><span style="color: #660066;" class="typ">DataBindingUtil</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">setContentView</span><span style="color: #666600;" class="pun">(</span><span style="color: #000088;" class="kwd">this</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln"> R</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">layout</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">main_activity</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                     </span><span style="color: #660066;" class="typ">User</span><span style="color: #000000;" class="pln"> user </span><span style="color: #666600;" class="pun">=</span><span style="color: #000088;" class="kwd">new</span><span style="color: #660066;" class="typ">User</span><span style="color: #666600;" class="pun">(</span><span style="color: #880000;" class="str">"Test"</span><span style="color: #666600;" class="pun">,</span><span style="color: #880000;" class="str">"User"</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                     binding</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">setUser</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">user</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                  </span><span style="color: #666600;" class="pun">}</span>


                  You’re done! Run the application and you’ll see Test User in the UI. Alternatively, you can get the view via:


                  1
                  <span style="color: #660066;" class="typ">MainActivityBinding</span><span style="color: #000000;" class="pln"> binding </span><span style="color: #666600;" class="pun">=</span><span style="color: #660066;" class="typ">MainActivityBinding</span><span style="color: #666600;" class="pun">.</span><em><span style="color: #000000;" class="pln">inflate</span></em><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">getLayoutInflater</span><span style="color: #666600;" class="pun">());</span>


                  If you are using data binding items inside a ListView or RecyclerView adapter, you may prefer to use:


                  1
                  2
                  3
                  <span style="color: #660066;" class="typ">ListItemBinding</span><span style="color: #000000;" class="pln"> binding </span><span style="color: #666600;" class="pun">=</span><span style="color: #660066;" class="typ">ListItemBinding</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">inflate</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">layoutInflater</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln"> viewGroup</span><span style="color: #666600;" class="pun">,</span><span style="color: #000088;" class="kwd">false</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                  </span><span class="com">//or</span><span style="color: #000000;" class="pln">
                  </span><span style="color: #660066;" class="typ">ListItemBinding</span><span style="color: #000000;" class="pln"> binding </span><span style="color: #666600;" class="pun">=</span><span style="color: #660066;" class="typ">DataBindingUtil</span><span style="color: #666600;" class="pun">.</span><em><span style="color: #000000;" class="pln">inflate</span></em><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">layoutInflater</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln"> R</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">layout</span><span style="color: #666600;" class="pun">.</span><em><strong style="color: inherit;"><span style="color: #000000;" class="pln">list_item</span>**</em><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln"> viewGroup</span><span style="color: #666600;" class="pun">,</span><strong style="color: inherit;"><span style="color: #000088;" class="kwd">false</span>**<span style="color: #666600;" class="pun">);</span>


                  Event Handling


                  Data Binding allows you to write expressions handling events that are dispatched from the views (e.g. onClick). Event attribute names are governed by the name of the listener method with a few exceptions. For example, has a method , so the attribute for this event is android:onLongClick. There are two ways to handle an event.




                  • : In your expressions, you can reference methods that conform to the signature of the listener method. When an expression evaluates to a method reference, Data Binding wraps the method reference and owner object in a listener, and sets that listener on the target view. If the expression evaluates to null, Data Binding does not create a listener and sets a null listener instead.

                  • : These are lambda expressions that are evaluated when the event happens. Data Binding always creates a listener, which it sets on the view. When the event is dispatched, the listener evaluates the lambda expression.

                    Method References


                    Events can be bound to handler methods directly, similar to the way android:onClick can be assigned to a method in an Activity. One major advantage compared to the View#onClick attribute is that the expression is processed at compile time, so if the method does not exist or its signature is not correct, you receive a compile time error.


                    The major difference between Method References and Listener Bindings is that the actual listener implementation is created when the data is bound, not when the event is triggered. If you prefer to evaluate the expression when the event happens, you should use .


                    To assign an event to its handler, use a normal binding expression, with the value being the method name to call. For example, if your data object has two methods:


                    1
                    2
                    3
                    <span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">class</span><span style="color: #660066;" class="typ">MyHandlers</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> onClickFriend</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">View</span><span style="color: #000000;" class="pln"> view</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #666600;" class="pun">...</span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                    </span><span style="color: #666600;" class="pun">}</span>


                    The binding expression can assign the click listener for a View:


                    1
                    2
                    3
                    4
                    5
                    6
                    7
                    8
                    9
                    10
                    11
                    12
                    13
                    14
                    15
                    16
                    <span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"1.0"</span><span style="color: #000000;" class="pln"> encoding</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"utf-8"</span><span style="color: #666600;" class="pun">?></span><span style="color: #000000;" class="pln">
                    </span><span style="color: #000088;" class="tag"><layout</span><span style="color: #882288;" class="atn">xmlns:android</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"http://schemas.android.com/apk/res/android"</span><span style="color: #000088;" class="tag">></span><span style="color: #000000;" class="pln">
                       </span><span style="color: #000088;" class="tag"><data></span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"handlers"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.example.Handlers"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"user"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.example.User"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                       </span><span style="color: #000088;" class="tag"></data></span><span style="color: #000000;" class="pln">
                       </span><span style="color: #000088;" class="tag"><LinearLayout</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:orientation</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"vertical"</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"match_parent"</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"match_parent"</span><span style="color: #000088;" class="tag">></span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="tag"><TextView</span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #882288;" class="atn">android:text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{user.firstName}"</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #882288;" class="atn">android:onClick</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{handlers::onClickFriend}"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                       </span><span style="color: #000088;" class="tag"></LinearLayout></span><span style="color: #000000;" class="pln">
                    </span><span style="color: #000088;" class="tag"></layout></span>


                    Note that the signature of the method in the expression must exactly match the signature of the method in the Listener object.


                    Listener Bindings


                    Listener Bindings are binding expressions that run when an event happens. They are similar to method references, but they let you run arbitrary data binding expressions. This feature is available with Android Gradle Plugin for Gradle version 2.0 and later.


                    In method references, the parameters of the method must match the parameters of the event listener. In Listener Bindings, only your return value must match the expected return value of the listener (unless it is expecting void). For example, you can have a presenter class that has the following method:


                    1
                    2
                    3
                    <span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">class</span><span style="color: #660066;" class="typ">Presenter</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> onSaveClick</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">Task</span><span style="color: #000000;" class="pln"> task</span><span style="color: #666600;" class="pun">){}</span><span style="color: #000000;" class="pln">
                    </span><span style="color: #666600;" class="pun">}</span>


                    Then you can bind the click event to your class as follows:
                    1
                    2
                    3
                    4
                    5
                    6
                    7
                    8
                    9
                    10
                    11
                    <span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"1.0"</span><span style="color: #000000;" class="pln"> encoding</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"utf-8"</span><span style="color: #666600;" class="pun">?></span><span style="color: #000000;" class="pln">
                      </span><span style="color: #000088;" class="tag"><layout</span><span style="color: #882288;" class="atn">xmlns:android</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"http://schemas.android.com/apk/res/android"</span><span style="color: #000088;" class="tag">></span><span style="color: #000000;" class="pln">
                          </span><span style="color: #000088;" class="tag"><data></span><span style="color: #000000;" class="pln">
                              </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"task"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.android.example.Task"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                              </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"presenter"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.android.example.Presenter"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                          </span><span style="color: #000088;" class="tag"></data></span><span style="color: #000000;" class="pln">
                          </span><span style="color: #000088;" class="tag"><LinearLayout</span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"match_parent"</span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"match_parent"</span><span style="color: #000088;" class="tag">></span><span style="color: #000000;" class="pln">
                              </span><span style="color: #000088;" class="tag"><Button</span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                              </span><span style="color: #882288;" class="atn">android:onClick</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{() -></span><span style="color: #000000;" class="pln"> presenter.onSaveClick(task)}" />
                          </span><span style="color: #000088;" class="tag"></LinearLayout></span><span style="color: #000000;" class="pln">
                      </span><span style="color: #000088;" class="tag"></layout></span>


                    Listeners are represented by lambda expressions that are allowed only as root elements of your expressions. When a callback is used in an expression, Data Binding automatically creates the necessary listener and registers for the event. When the view fires the event, Data Binding evaluates the given expression. As in regular binding expressions, you still get the null and thread safety of Data Binding while these listener expressions are being evaluated.


                    Note that in the example above, we haven’t defined the view parameter that is passed into . Listener bindings provide two choices for listener parameters: you can either ignore all parameters to the method or name all of them. If you prefer to name the parameters, you can use them in your expression. For example, the expression above could be written as:


                    1
                    <span style="color: #000000;" class="pln">  android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">onClick</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"@{(view) -> presenter.onSaveClick(task)}"</span>


                    Or if you wanted to use the parameter in the expression, it could work as follows:
                    1
                    2
                    3
                    <span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">class</span><span style="color: #660066;" class="typ">Presenter</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> onSaveClick</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">View</span><span style="color: #000000;" class="pln"> view</span><span style="color: #666600;" class="pun">,</span><span style="color: #660066;" class="typ">Task</span><span style="color: #000000;" class="pln"> task</span><span style="color: #666600;" class="pun">){}</span><span style="color: #000000;" class="pln">
                    </span><span style="color: #666600;" class="pun">}</span>


                    1
                    <span style="color: #000000;" class="pln">  android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">onClick</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"@{(theView) -> presenter.onSaveClick(theView, task)}"</span>


                    You can use a lambda expression with more than one parameter:
                    1
                    2
                    3
                    <span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">class</span><span style="color: #660066;" class="typ">Presenter</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> onCompletedChanged</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">Task</span><span style="color: #000000;" class="pln"> task</span><span style="color: #666600;" class="pun">,</span><span style="color: #000088;" class="kwd">boolean</span><span style="color: #000000;" class="pln"> completed</span><span style="color: #666600;" class="pun">){}</span><span style="color: #000000;" class="pln">
                    </span><span style="color: #666600;" class="pun">}</span>


                    1
                    2
                    <span style="color: #000000;" class="pln">  </span><span style="color: #000088;" class="tag"><CheckBox</span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                            </span><span style="color: #882288;" class="atn">android:onCheckedChanged</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{(cb, isChecked) -></span><span style="color: #000000;" class="pln"> presenter.completeChanged(task, isChecked)}" /></span>


                    If the event you are listening to returns a value whose type is not void, your expressions must return the same type of value as well. For example, if you want to listen for the long click event, your expression should return boolean.


                    1
                    2
                    3
                    <span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">class</span><span style="color: #660066;" class="typ">Presenter</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">boolean</span><span style="color: #000000;" class="pln"> onLongClick</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">View</span><span style="color: #000000;" class="pln"> view</span><span style="color: #666600;" class="pun">,</span><span style="color: #660066;" class="typ">Task</span><span style="color: #000000;" class="pln"> task</span><span style="color: #666600;" class="pun">){}</span><span style="color: #000000;" class="pln">
                    </span><span style="color: #666600;" class="pun">}</span>


                    1
                    <span style="color: #000000;" class="pln">  android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">onLongClick</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"@{(theView) -> presenter.onLongClick(theView, task)}"</span>


                    If the expression cannot be evaluated due to null objects, Data Binding returns the default Java value for that type. For example, null for reference types, 0 for int, false for boolean, etc.


                    If you need to use an expression with a predicate (e.g. ternary), you can use void as a symbol.


                    1
                    <span style="color: #000000;" class="pln">  android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">onClick</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"@{(v) -> v.isVisible() ? doSomething() : void}"</span>


                    Avoid Complex Listeners

                    Listener expressions are very powerful and can make your code very easy to read. On the other hand, listeners containing complex expressions make your layouts hard to read and unmaintainable. These expressions should be as simple as passing available data from your UI to your callback method. You should implement any business logic inside the callback method that you invoked from the listener expression.

                    Some specialized click event handlers exist and they need an attribute other than android:onClick to avoid a conflict. The following attributes have been created to avoid such conflicts:




                    Class
                    Listener Setter
                    Attribute
















                    android:onSearchClick
                    android:onZoomIn
                    android:onZoomOut

                    Layout Details




                    Imports


                    Zero or more import elements may be used inside the data element. These allow easy reference to classes inside your layout file, just like in Java.


                    1
                    2
                    3
                    <span style="color: #000088;" class="tag"><data></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"><import</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"android.view.View"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                    </span><span style="color: #000088;" class="tag"></data></span>


                    Now, View may be used within your binding expression:


                    1
                    2
                    3
                    4
                    5
                    <span style="color: #000088;" class="tag"><TextView</span><span style="color: #000000;" class="pln">
                       </span><span style="color: #882288;" class="atn">android:text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{user.lastName}"</span><span style="color: #000000;" class="pln">
                       </span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                       </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                       </span><span style="color: #882288;" class="atn">android:visibility</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{user.isAdult ? View.VISIBLE : View.GONE}"</span><span style="color: #000088;" class="tag">/></span>


                    When there are class name conflicts, one of the classes may be renamed to an “alias:”


                    1
                    2
                    3
                    <span style="color: #000088;" class="tag"><import</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"android.view.View"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                    </span><span style="color: #000088;" class="tag"><import</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.example.real.estate.View"</span><span style="color: #000000;" class="pln">
                            </span><span style="color: #882288;" class="atn">alias</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"Vista"</span><span style="color: #000088;" class="tag">/></span>


                    Now, Vista may be used to reference the com.example.real.estate.View and View may be used to referenceandroid.view.View within the layout file. Imported types may be used as type references in variables and expressions:


                    1
                    2
                    3
                    4
                    5
                    6
                    <span style="color: #000088;" class="tag"><data></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"><import</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.example.User"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"><import</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"java.util.List"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"user"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"User"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"userList"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"List&lt;User&gt;"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                    </span><span style="color: #000088;" class="tag"></data></span>


                    Note: Android Studio does not yet handle imports so the autocomplete for imported variables may not work in your IDE. Your application will still compile fine and you can work around the IDE issue by using fully qualified names in your variable definitions.


                    1
                    2
                    3
                    4
                    <span style="color: #000088;" class="tag"><TextView</span><span style="color: #000000;" class="pln">
                       </span><span style="color: #882288;" class="atn">android:text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{((User)(user.connection)).lastName}"</span><span style="color: #000000;" class="pln">
                       </span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                       </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000088;" class="tag">/></span>


                    Imported types may also be used when referencing static fields and methods in expressions:


                    1
                    2
                    3
                    4
                    5
                    6
                    7
                    8
                    9
                    <span style="color: #000088;" class="tag"><data></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"><import</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.example.MyStringUtils"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"user"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.example.User"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                    </span><span style="color: #000088;" class="tag"></data></span><span style="color: #000000;" class="pln">

                    </span><span style="color: #000088;" class="tag"><TextView</span><span style="color: #000000;" class="pln">
                       </span><span style="color: #882288;" class="atn">android:text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{MyStringUtils.capitalize(user.lastName)}"</span><span style="color: #000000;" class="pln">
                       </span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                       </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000088;" class="tag">/></span>


                    Just as in Java, java.lang. is imported automatically.


                    Variables


                    Any number of variable elements may be used inside the data element. Each variable element describes a property that may be set on the layout to be used in binding expressions within the layout file.


                    1
                    2
                    3
                    4
                    5
                    6
                    <span style="color: #000088;" class="tag"><data></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"><import</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"android.graphics.drawable.Drawable"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"user"</span><span style="color: #000000;" class="pln">  </span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.example.User"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"image"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"Drawable"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"note"</span><span style="color: #000000;" class="pln">  </span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"String"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                    </span><span style="color: #000088;" class="tag"></data></span>


                    The variable types are inspected at compile time, so if a variable implements or is an , that should be reflected in the type. If the variable is a base class or interface that does not implement the Observable interface, the variables will not be observed!


                    When there are different layout files for various configurations (e.g. landscape or portrait), the variables will be combined. There must not be conflicting variable definitions between these layout files.


                    The generated binding class will have a setter and getter for each of the described variables. The variables will take the default Java values until the setter is called — null for reference types, 0 for int, false for boolean, etc.


                    A special variable named context is generated for use in binding expressions as needed. The value for context is the Context from the root View’s . The context variable will be overridden by an explicit variable declaration with that name.


                    Custom Binding Class Names


                    By default, a Binding class is generated based on the name of the layout file, starting it with upper-case, removing underscores ( ) and capitalizing the following letter and then suffixing “Binding”. This class will be placed in a databinding package under the module package. For example, the layout file contact_item.xml will generateContactItemBinding. If the module package is com.example.my.app, then it will be placed incom.example.my.app.databinding.


                    Binding classes may be renamed or placed in different packages by adjusting the class attribute of the dataelement. For example:


                    1
                    2
                    3
                    <span style="color: #000088;" class="tag"><data</span><span style="color: #882288;" class="atn">class</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"ContactItem"</span><span style="color: #000088;" class="tag">></span><span style="color: #000000;" class="pln">
                        ...
                    </span><span style="color: #000088;" class="tag"></data></span>


                    This generates the binding class as ContactItem in the databinding package in the module package. If the class should be generated in a different package within the module package, it may be prefixed with “.”:


                    1
                    2
                    3
                    <span style="color: #000088;" class="tag"><data</span><span style="color: #882288;" class="atn">class</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">".ContactItem"</span><span style="color: #000088;" class="tag">></span><span style="color: #000000;" class="pln">
                        ...
                    </span><span style="color: #000088;" class="tag"></data></span>


                    In this case, ContactItem is generated in the module package directly. Any package may be used if the full package is provided:


                    1
                    2
                    3
                    <span style="color: #000088;" class="tag"><data</span><span style="color: #882288;" class="atn">class</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.example.ContactItem"</span><span style="color: #000088;" class="tag">></span><span style="color: #000000;" class="pln">
                        ...
                    </span><span style="color: #000088;" class="tag"></data></span>


                    Includes


                    Variables may be passed into an included layout’s binding from the containing layout by using the application namespace and the variable name in an attribute:


                    1
                    2
                    3
                    4
                    5
                    6
                    7
                    8
                    9
                    10
                    11
                    12
                    13
                    14
                    15
                    16
                    <span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"1.0"</span><span style="color: #000000;" class="pln"> encoding</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"utf-8"</span><span style="color: #666600;" class="pun">?></span><span style="color: #000000;" class="pln">
                    </span><span style="color: #000088;" class="tag"><layout</span><span style="color: #882288;" class="atn">xmlns:android</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"http://schemas.android.com/apk/res/android"</span><span style="color: #000000;" class="pln">
                            </span><span style="color: #882288;" class="atn">xmlns:bind</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"http://schemas.android.com/apk/res-auto"</span><span style="color: #000088;" class="tag">></span><span style="color: #000000;" class="pln">
                       </span><span style="color: #000088;" class="tag"><data></span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"user"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.example.User"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                       </span><span style="color: #000088;" class="tag"></data></span><span style="color: #000000;" class="pln">
                       </span><span style="color: #000088;" class="tag"><LinearLayout</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:orientation</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"vertical"</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"match_parent"</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"match_parent"</span><span style="color: #000088;" class="tag">></span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="tag"><include</span><span style="color: #882288;" class="atn">layout</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@layout/name"</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #882288;" class="atn">bind:user</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{user}"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="tag"><include</span><span style="color: #882288;" class="atn">layout</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@layout/contact"</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #882288;" class="atn">bind:user</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{user}"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                       </span><span style="color: #000088;" class="tag"></LinearLayout></span><span style="color: #000000;" class="pln">
                    </span><span style="color: #000088;" class="tag"></layout></span>


                    Here, there must be a user variable in both the name.xml and contact.xml layout files.


                    Data binding does not support include as a direct child of a merge element. For example, the following layout is not supported:


                    1
                    2
                    3
                    4
                    5
                    6
                    7
                    8
                    9
                    10
                    11
                    12
                    13
                    <span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"1.0"</span><span style="color: #000000;" class="pln"> encoding</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"utf-8"</span><span style="color: #666600;" class="pun">?></span><span style="color: #000000;" class="pln">
                    </span><span style="color: #000088;" class="tag"><layout</span><span style="color: #882288;" class="atn">xmlns:android</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"http://schemas.android.com/apk/res/android"</span><span style="color: #000000;" class="pln">
                            </span><span style="color: #882288;" class="atn">xmlns:bind</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"http://schemas.android.com/apk/res-auto"</span><span style="color: #000088;" class="tag">></span><span style="color: #000000;" class="pln">
                       </span><span style="color: #000088;" class="tag"><data></span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"user"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.example.User"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                       </span><span style="color: #000088;" class="tag"></data></span><span style="color: #000000;" class="pln">
                       </span><span style="color: #000088;" class="tag"><merge></span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="tag"><include</span><span style="color: #882288;" class="atn">layout</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@layout/name"</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #882288;" class="atn">bind:user</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{user}"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="tag"><include</span><span style="color: #882288;" class="atn">layout</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@layout/contact"</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #882288;" class="atn">bind:user</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{user}"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                       </span><span style="color: #000088;" class="tag"></merge></span><span style="color: #000000;" class="pln">
                    </span><span style="color: #000088;" class="tag"></layout></span>


                    Expression Language


                    Common Features


                    The expression language looks a lot like a Java expression. These are the same:



                    • Mathematical + - / * %

                    • String concatenation +

                    • Logical && ||

                    • Binary & | ^

                    • Unary + - ! ~

                    • Shift >> >>> <<

                    • Comparison == > < >= <=

                    • instanceof
                    • Grouping ()

                    • Literals - character, String, numeric, null

                    • Cast
                    • Method calls
                    • Field access
                    • Array access []

                    • Ternary operator ?:


                      Examples:


                      1
                      2
                      3
                      <span style="color: #000000;" class="pln">android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"@{String.valueOf(index + 1)}"</span><span style="color: #000000;" class="pln">
                      android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">visibility</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"@{age < 13 ? View.GONE : View.VISIBLE}"</span><span style="color: #000000;" class="pln">
                      android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">transitionName</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">'@{"image_" + id}'</span>


                      Missing Operations


                      A few operations are missing from the expression syntax that you can use in Java.



                      • this
                      • super
                      • new
                      • Explicit generic invocation

                        Null Coalescing Operator


                        The null coalescing operator (??) chooses the left operand if it is not null or the right if it is null.


                        1
                        <strong style="color: inherit;"><span style="color: #000000;" class="pln">android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"@{user.displayName ?? user.lastName}"</span>**


                        This is functionally equivalent to:


                        1
                        <strong style="color: inherit;"><span style="color: #000000;" class="pln">android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"@{user.displayName != null ? user.displayName : user.lastName}"</span>**


                        Property Reference


                        The first was already discussed in the above: short form JavaBean references. When an expression references a property on a class, it uses the same format for fields, getters, and ObservableFields.


                        1
                        <strong style="color: inherit;"><span style="color: #000000;" class="pln">android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"@{user.lastName}"</span>**


                        Avoiding NullPointerException


                        Generated data binding code automatically checks for nulls and avoid null pointer exceptions. For example, in the expression @{user.name}, if user is null, user.name will be assigned its default value (null). If you were referencing user.age, where age is an int, then it would default to 0.


                        Collections


                        Common collections: arrays, lists, sparse lists, and maps, may be accessed using the [] operator for convenience.


                        1
                        2
                        3
                        4
                        5
                        6
                        7
                        8
                        9
                        10
                        11
                        12
                        13
                        14
                        15
                        16
                        17
                        <span style="color: #000088;" class="tag"><data></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><import</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"android.util.SparseArray"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><import</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"java.util.Map"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><import</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"java.util.List"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"list"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"List&lt;String&gt;"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"sparse"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"SparseArray&lt;String&gt;"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"map"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"Map&lt;String, String&gt;"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"index"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"int"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"key"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"String"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"></data></span><span style="color: #000000;" class="pln">

                        android:text="@{list[index]}"

                        android:text="@{sparse[index]}"

                        android:text="@{map[key]}"
                        </span>


                        String Literals


                        When using single quotes around the attribute value, it is easy to use double quotes in the expression:


                        1
                        <span style="color: #000000;" class="pln">android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">'@{map["firstName"]}'</span>


                        It is also possible to use double quotes to surround the attribute value. When doing so, String literals should either use the ‘ or back quote (`).


                        1
                        2
                        <span style="color: #000000;" class="pln">android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"@{map[`firstName`}"</span><span style="color: #000000;" class="pln">
                        android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"@{map['firstName']}"</span>


                        Resources


                        It is possible to access resources as part of expressions using the normal syntax:


                        1
                        <span style="color: #000000;" class="pln">android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">padding</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"@{large? @dimen/largePadding : @dimen/smallPadding}"</span>


                        Format strings and plurals may be evaluated by providing parameters:


                        1
                        2
                        <span style="color: #000000;" class="pln">android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"@{@string/nameFormat(firstName, lastName)}"</span><span style="color: #000000;" class="pln">
                        android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"@{@plurals/banana(bananaCount)}"</span>


                        When a plural takes multiple parameters, all parameters should be passed:


                        1
                        2
                        3
                        4
                        <span style="color: #000000;" class="pln">
                          </span><span style="color: #660066;" class="typ">Have</span><span style="color: #000000;" class="pln"> an orange
                          </span><span style="color: #660066;" class="typ">Have</span><span style="color: #666600;" class="pun">%</span><span style="color: #000000;" class="pln">d oranges
                        android</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"@{@plurals/orange(orangeCount, orangeCount)}"</span>


                        Some resources require explicit type evaluation.




                        Type
                        Normal Reference
                        Expression Reference




































                        String[]@array@stringArray
                        int[]@array@intArray
                        TypedArray@array@typedArray
                        Animator@animator@animator
                        StateListAnimator@animator@stateListAnimator
                        color int
                        @color@color
                        ColorStateList@color@colorStateList

                        Data Objects




                        Any plain old Java object (POJO) may be used for data binding, but modifying a POJO will not cause the UI to update. The real power of data binding can be used by giving your data objects the ability to notify when data changes. There are three different data change notification mechanisms, , , ands.


                        When one of these observable data object is bound to the UI and a property of the data object changes, the UI will be updated automatically.


                        Observable Objects


                        A class implementing the interface will allow the binding to attach a single listener to a bound object to listen for changes of all properties on that object.


                        The interface has a mechanism to add and remove listeners, but notifying is up to the developer. To make development easier, a base class, , was created to implement the listener registration mechanism. The data class implementer is still responsible for notifying when the properties change. This is done by assigning a annotation to the getter and notifying in the setter.


                        1
                        2
                        3
                        4
                        5
                        6
                        7
                        8
                        9
                        10
                        11
                        12
                        13
                        14
                        15
                        16
                        17
                        18
                        19
                        20
                        <span style="color: #000088;" class="kwd">private</span><span style="color: #000088;" class="kwd">static</span><span style="color: #000088;" class="kwd">class</span><span style="color: #660066;" class="typ">User</span><span style="color: #000088;" class="kwd">extends</span><span style="color: #660066;" class="typ">BaseObservable</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="kwd">private</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> firstName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="kwd">private</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> lastName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #006666;" class="lit">@Bindable</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="kwd">public</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> getFirstName</span><span style="color: #666600;" class="pun">()</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #000088;" class="kwd">return</span><span style="color: #000088;" class="kwd">this</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">firstName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #006666;" class="lit">@Bindable</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="kwd">public</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> getLastName</span><span style="color: #666600;" class="pun">()</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #000088;" class="kwd">return</span><span style="color: #000088;" class="kwd">this</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">lastName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> setFirstName</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> firstName</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #000088;" class="kwd">this</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">firstName </span><span style="color: #666600;" class="pun">=</span><span style="color: #000000;" class="pln"> firstName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                               notifyPropertyChanged</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">BR</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">firstName</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> setLastName</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> lastName</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #000088;" class="kwd">this</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">lastName </span><span style="color: #666600;" class="pun">=</span><span style="color: #000000;" class="pln"> lastName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                               notifyPropertyChanged</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">BR</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">lastName</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #666600;" class="pun">}</span>


                        The annotation generates an entry in the BR class file during compilation. The BR class file will be generated in the module package. If the base class for data classes cannot be changed, the interface may be implemented using the convenient to store and notify listeners efficiently.


                        ObservableFields


                        A little work is involved in creating classes, so developers who want to save time or have few properties may use and its siblings , , ,, , , , , and. ObservableFields are self-contained observable objects that have a single field. The primitive versions avoid boxing and unboxing during access operations. To use, create a public final field in the data class:


                        1
                        2
                        3
                        4
                        5
                        6
                        7
                        <span style="color: #000088;" class="kwd">private</span><span style="color: #000088;" class="kwd">static</span><span style="color: #000088;" class="kwd">class</span><span style="color: #660066;" class="typ">User</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">final</span><span style="color: #660066;" class="typ">ObservableField</span><span style="color: #666600;" class="pun"><</span><span style="color: #660066;" class="typ">String</span><span style="color: #666600;" class="pun">></span><span style="color: #000000;" class="pln"> firstName </span><span style="color: #666600;" class="pun">=</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #000088;" class="kwd">new</span><span style="color: #660066;" class="typ">ObservableField</span><span style="color: #666600;" class="pun"><>();</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">final</span><span style="color: #660066;" class="typ">ObservableField</span><span style="color: #666600;" class="pun"><</span><span style="color: #660066;" class="typ">String</span><span style="color: #666600;" class="pun">></span><span style="color: #000000;" class="pln"> lastName </span><span style="color: #666600;" class="pun">=</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #000088;" class="kwd">new</span><span style="color: #660066;" class="typ">ObservableField</span><span style="color: #666600;" class="pun"><>();</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">final</span><span style="color: #660066;" class="typ">ObservableInt</span><span style="color: #000000;" class="pln"> age </span><span style="color: #666600;" class="pun">=</span><span style="color: #000088;" class="kwd">new</span><span style="color: #660066;" class="typ">ObservableInt</span><span style="color: #666600;" class="pun">();</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #666600;" class="pun">}</span>


                        That’s it! To access the value, use the set and get accessor methods:


                        1
                        2
                        <span style="color: #000000;" class="pln">user</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">firstName</span><span style="color: #666600;" class="pun">.</span><span style="color: #000088;" class="kwd">set</span><span style="color: #666600;" class="pun">(</span><span style="color: #880000;" class="str">"Google"</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="kwd">int</span><span style="color: #000000;" class="pln"> age </span><span style="color: #666600;" class="pun">=</span><span style="color: #000000;" class="pln"> user</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">age</span><span style="color: #666600;" class="pun">.</span><span style="color: #000088;" class="kwd">get</span><span style="color: #666600;" class="pun">();</span>


                        Observable Collections


                        Some applications use more dynamic structures to hold data. Observable collections allow keyed access to these data objects. is useful when the key is a reference type, such as String.


                        1
                        2
                        3
                        4
                        <span style="color: #660066;" class="typ">ObservableArrayMap</span><span style="color: #666600;" class="pun"><</span><span style="color: #660066;" class="typ">String</span><span style="color: #666600;" class="pun">,</span><span style="color: #660066;" class="typ">Object</span><span style="color: #666600;" class="pun">></span><span style="color: #000000;" class="pln"> user </span><span style="color: #666600;" class="pun">=</span><span style="color: #000088;" class="kwd">new</span><span style="color: #660066;" class="typ">ObservableArrayMap</span><span style="color: #666600;" class="pun"><>();</span><span style="color: #000000;" class="pln">
                        user</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">put</span><span style="color: #666600;" class="pun">(</span><span style="color: #880000;" class="str">"firstName"</span><span style="color: #666600;" class="pun">,</span><span style="color: #880000;" class="str">"Google"</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                        user</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">put</span><span style="color: #666600;" class="pun">(</span><span style="color: #880000;" class="str">"lastName"</span><span style="color: #666600;" class="pun">,</span><span style="color: #880000;" class="str">"Inc."</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                        user</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">put</span><span style="color: #666600;" class="pun">(</span><span style="color: #880000;" class="str">"age"</span><span style="color: #666600;" class="pun">,</span><span style="color: #006666;" class="lit">17</span><span style="color: #666600;" class="pun">);</span>


                        In the layout, the map may be accessed through the String keys:


                        1
                        2
                        3
                        4
                        5
                        6
                        7
                        8
                        9
                        10
                        11
                        12
                        13
                        <span style="color: #000088;" class="tag"><data></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><import</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"android.databinding.ObservableMap"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"user"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"ObservableMap&lt;String, Object&gt;"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"></data></span><span style="color: #000000;" class="pln">

                        </span><span style="color: #000088;" class="tag"><TextView</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">'@{user["lastName"]}'</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"><TextView</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">'@{String.valueOf(1 + (Integer)user["age"])}'</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000088;" class="tag">/></span>


                        is useful when the key is an integer:


                        1
                        2
                        3
                        4
                        <span style="color: #660066;" class="typ">ObservableArrayList</span><span style="color: #666600;" class="pun"><</span><span style="color: #660066;" class="typ">Object</span><span style="color: #666600;" class="pun">></span><span style="color: #000000;" class="pln"> user </span><span style="color: #666600;" class="pun">=</span><span style="color: #000088;" class="kwd">new</span><span style="color: #660066;" class="typ">ObservableArrayList</span><span style="color: #666600;" class="pun"><>();</span><span style="color: #000000;" class="pln">
                        user</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">add</span><span style="color: #666600;" class="pun">(</span><span style="color: #880000;" class="str">"Google"</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                        user</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">add</span><span style="color: #666600;" class="pun">(</span><span style="color: #880000;" class="str">"Inc."</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                        user</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">add</span><span style="color: #666600;" class="pun">(</span><span style="color: #006666;" class="lit">17</span><span style="color: #666600;" class="pun">);</span>


                        In the layout, the list may be accessed through the indices:


                        1
                        2
                        3
                        4
                        5
                        6
                        7
                        8
                        9
                        10
                        11
                        12
                        13
                        14
                        <span style="color: #000088;" class="tag"><data></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><import</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"android.databinding.ObservableList"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><import</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.example.my.app.Fields"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"user"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"ObservableList&lt;Object&gt;"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"></data></span><span style="color: #000000;" class="pln">

                        </span><span style="color: #000088;" class="tag"><TextView</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">'@{user[Fields.LAST_NAME]}'</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"><TextView</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">'@{String.valueOf(1 + (Integer)user[Fields.AGE])}'</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000088;" class="tag">/></span>


                        Generated Binding




                        The generated binding class links the layout variables with the Views within the layout. As discussed earlier, the name and package of the Binding may be . The Generated binding classes all extend .


                        Creating


                        The binding should be created soon after inflation to ensure that the View hierarchy is not disturbed prior to binding to the Views with expressions within the layout. There are a few ways to bind to a layout. The most common is to use the static methods on the Binding class.The inflate method inflates the View hierarchy and binds to it all it one step. There is a simpler version that only takes a and one that takes a as well:


                        1
                        2
                        <span style="color: #660066;" class="typ">MyLayoutBinding</span><span style="color: #000000;" class="pln"> binding </span><span style="color: #666600;" class="pun">=</span><span style="color: #660066;" class="typ">MyLayoutBinding</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">inflate</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">layoutInflater</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #660066;" class="typ">MyLayoutBinding</span><span style="color: #000000;" class="pln"> binding </span><span style="color: #666600;" class="pun">=</span><span style="color: #660066;" class="typ">MyLayoutBinding</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">inflate</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">layoutInflater</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln"> viewGroup</span><span style="color: #666600;" class="pun">,</span><span style="color: #000088;" class="kwd">false</span><span style="color: #666600;" class="pun">);</span>


                        If the layout was inflated using a different mechanism, it may be bound separately:


                        1
                        <span style="color: #660066;" class="typ">MyLayoutBinding</span><span style="color: #000000;" class="pln"> binding </span><span style="color: #666600;" class="pun">=</span><span style="color: #660066;" class="typ">MyLayoutBinding</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">bind</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">viewRoot</span><span style="color: #666600;" class="pun">);</span>


                        Sometimes the binding cannot be known in advance. In such cases, the binding can be created using the class:


                        1
                        2
                        3
                        <span style="color: #660066;" class="typ">ViewDataBinding</span><span style="color: #000000;" class="pln"> binding </span><span style="color: #666600;" class="pun">=</span><span style="color: #660066;" class="typ">DataBindingUtil</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">inflate</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">LayoutInflater</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln"> layoutId</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln">
                            parent</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln"> attachToParent</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #660066;" class="typ">ViewDataBinding</span><span style="color: #000000;" class="pln"> binding </span><span style="color: #666600;" class="pun">=</span><span style="color: #660066;" class="typ">DataBindingUtil</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">bindTo</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">viewRoot</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln"> layoutId</span><span style="color: #666600;" class="pun">);</span>


                        Views With IDs


                        A public final field will be generated for each View with an ID in the layout. The binding does a single pass on the View hierarchy, extracting the Views with IDs. This mechanism can be faster than calling findViewById for several Views. For example:


                        1
                        2
                        3
                        4
                        5
                        6
                        7
                        8
                        9
                        10
                        11
                        12
                        13
                        14
                        15
                        16
                        17
                        18
                        <span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"http://schemas.android.com/apk/res/android"</span><span style="color: #000088;" class="tag">></span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="tag"><data></span><span style="color: #000000;" class="pln">
                               </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"user"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.example.User"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="tag"></data></span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="tag"><LinearLayout</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #882288;" class="atn">android:orientation</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"vertical"</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"match_parent"</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"match_parent"</span><span style="color: #000088;" class="tag">></span><span style="color: #000000;" class="pln">
                               </span><span style="color: #000088;" class="tag"><TextView</span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                                   </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                                   </span><span style="color: #882288;" class="atn">android:text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{user.firstName}"</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #882288;" class="atn">android:id</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@+id/firstName"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                               </span><span style="color: #000088;" class="tag"><TextView</span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                                   </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                                   </span><span style="color: #882288;" class="atn">android:text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{user.lastName}"</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #882288;" class="atn">android:id</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@+id/lastName"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="tag"></LinearLayout></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"></layout></span>


                        Will generate a binding class with:


                        1
                        2
                        <span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">final</span><span style="color: #660066;" class="typ">TextView</span><span style="color: #000000;" class="pln"> firstName</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">final</span><span style="color: #660066;" class="typ">TextView</span><span style="color: #000000;" class="pln"> lastName</span><span style="color: #666600;" class="pun">;</span>


                        IDs are not nearly as necessary as without data binding, but there are still some instances where access to Views are still necessary from code.


                        Variables


                        Each variable will be given accessor methods.


                        1
                        2
                        3
                        4
                        5
                        6
                        <span style="color: #000088;" class="tag"><data></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><import</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"android.graphics.drawable.Drawable"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"user"</span><span style="color: #000000;" class="pln">  </span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"com.example.User"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"image"</span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"Drawable"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                            </span><span style="color: #000088;" class="tag"><variable</span><span style="color: #882288;" class="atn">name</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"note"</span><span style="color: #000000;" class="pln">  </span><span style="color: #882288;" class="atn">type</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"String"</span><span style="color: #000088;" class="tag">/></span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="tag"></data></span>


                        will generate setters and getters in the binding:


                        1
                        2
                        3
                        4
                        5
                        6
                        <span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">abstract</span><span style="color: #000000;" class="pln"> com</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">example</span><span style="color: #666600;" class="pun">.</span><span style="color: #660066;" class="typ">User</span><span style="color: #000000;" class="pln"> getUser</span><span style="color: #666600;" class="pun">();</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">abstract</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> setUser</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">com</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">example</span><span style="color: #666600;" class="pun">.</span><span style="color: #660066;" class="typ">User</span><span style="color: #000000;" class="pln"> user</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">abstract</span><span style="color: #660066;" class="typ">Drawable</span><span style="color: #000000;" class="pln"> getImage</span><span style="color: #666600;" class="pun">();</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">abstract</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> setImage</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">Drawable</span><span style="color: #000000;" class="pln"> image</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">abstract</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> getNote</span><span style="color: #666600;" class="pun">();</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">abstract</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> setNote</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> note</span><span style="color: #666600;" class="pun">);</span>


                        ViewStubs


                        s are a little different from normal Views. They start off invisible and when they either are made visible or are explicitly told to inflate, they replace themselves in the layout by inflating another layout.


                        Because the ViewStub essentially disappears from the View hierarchy, the View in the binding object must also disappear to allow collection. Because the Views are final, a object takes the place of theViewStub, giving the developer access to the ViewStub when it exists and also access to the inflated View hierarchy when the ViewStub has been inflated.


                        When inflating another layout, a binding must be established for the new layout. Therefore, the ViewStubProxymust listen to the ViewStub‘s and establish the binding at that time. Since only one can exist, the ViewStubProxy allows the developer to set an OnInflateListener on it that it will call after establishing the binding.


                        Advanced Binding


                        Dynamic Variables


                        At times, the specific binding class won’t be known. For example, a operating against arbitrary layouts won’t know the specific binding class. It still must assign the binding value during the.


                        In this example, all layouts that the RecyclerView binds to have an “item” variable. The BindingHolder has a getBinding method returning the base.


                        1
                        2
                        3
                        4
                        5
                        <span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> onBindViewHolder</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">BindingHolder</span><span style="color: #000000;" class="pln"> holder</span><span style="color: #666600;" class="pun">,</span><span style="color: #000088;" class="kwd">int</span><span style="color: #000000;" class="pln"> position</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #000088;" class="kwd">final</span><span style="color: #000000;" class="pln"> T item </span><span style="color: #666600;" class="pun">=</span><span style="color: #000000;" class="pln"> mItems</span><span style="color: #666600;" class="pun">.</span><span style="color: #000088;" class="kwd">get</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">position</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                           holder</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">getBinding</span><span style="color: #666600;" class="pun">().</span><span style="color: #000000;" class="pln">setVariable</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">BR</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">item</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln"> item</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                           holder</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">getBinding</span><span style="color: #666600;" class="pun">().</span><span style="color: #000000;" class="pln">executePendingBindings</span><span style="color: #666600;" class="pun">();</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #666600;" class="pun">}</span>


                        Immediate Binding


                        When a variable or observable changes, the binding will be scheduled to change before the next frame. There are times, however, when binding must be executed immediately. To force execution, use the method.


                        Background Thread


                        You can change your data model in a background thread as long as it is not a collection. Data binding will localize each variable / field while evaluating to avoid any concurrency issues.


                        Attribute Setters




                        Whenever a bound value changes, the generated binding class must call a setter method on the View with the binding expression. The data binding framework has ways to customize which method to call to set the value.


                        Automatic Setters


                        For an attribute, data binding tries to find the method setAttribute. The namespace for the attribute does not matter, only the attribute name itself.

                        For example, an expression associated with TextView’s attribute android:text will look for a setText(String). If the expression returns an int, data binding will search for a setText(int) method. Be careful to have the expression return the correct type, casting if necessary. Note that data binding will work even if no attribute exists with the given name. You can then easily “create” attributes for any setter by using data binding. For example, support DrawerLayout doesn’t have any attributes, but plenty of setters. You can use the automatic setters to use one of these.


                        1
                        2
                        3
                        4
                        5
                        <span style="color: #000088;" class="tag"><android.support.v4.widget.</span><strong style="color: inherit;"><span style="color: #000088;" class="tag">DrawerLayout</span><span style="color: #000000;" class="pln">
                            </span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                            </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                            </span><span style="color: #882288;" class="atn">app:scrimColor</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{@color/scrim}"</span><span style="color: #000000;" class="pln">
                            </span><span style="color: #882288;" class="atn">app:drawerListener</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{fragment.drawerListener}"</span><span style="color: #000088;" class="tag">/></span>**


                        Renamed Setters


                        Some attributes have setters that don’t match by name. For these methods, an attribute may be associated with the setter through annotation. This must be associated with a class and contains annotations, one for each renamed method. For example, the android:tint attribute is really associated with , not setTint.


                        1
                        2
                        3
                        4
                        5
                        <span style="color: #006666;" class="lit">@BindingMethods</span><span style="color: #666600;" class="pun">({</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #006666;" class="lit">@BindingMethod</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">type </span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"android.widget.ImageView"</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln">
                                              attribute </span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"android:tint"</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln">
                                              method </span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="str">"setImageTintList"</span><span style="color: #666600;" class="pun">),</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #666600;" class="pun">})</span>


                        It is unlikely that developers will need to rename setters; the android framework attributes have already been implemented.


                        Custom Setters


                        Some attributes need custom binding logic. For example, there is no associated setter for the android:paddingLeft attribute. Instead, setPadding(left, top, right, bottom) exists. A static binding adapter method with the annotation allows the developer to customize how a setter for an attribute is called.


                        The android attributes have already had BindingAdapters created. For example, here is the one for paddingLeft:


                        1
                        2
                        3
                        4
                        5
                        6
                        7
                        <span style="color: #006666;" class="lit">@BindingAdapter</span><span style="color: #666600;" class="pun">(</span><span style="color: #880000;" class="str">"android:paddingLeft"</span><span style="color: #666600;" class="pun">)</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">static</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> setPaddingLeft</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">View</span><span style="color: #000000;" class="pln"> view</span><span style="color: #666600;" class="pun">,</span><span style="color: #000088;" class="kwd">int</span><span style="color: #000000;" class="pln"> padding</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                           view</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">setPadding</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">padding</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln">
                                           view</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">getPaddingTop</span><span style="color: #666600;" class="pun">(),</span><span style="color: #000000;" class="pln">
                                           view</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">getPaddingRight</span><span style="color: #666600;" class="pun">(),</span><span style="color: #000000;" class="pln">
                                           view</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">getPaddingBottom</span><span style="color: #666600;" class="pun">());</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #666600;" class="pun">}</span>


                        Binding adapters are useful for other types of customization. For example, a custom loader can be called off-thread to load an image.


                        Developer-created binding adapters will override the data binding default adapters when there is a conflict.


                        You can also have adapters that receive multiple parameters.


                        1
                        2
                        3
                        4
                        <span style="color: #006666;" class="lit">@BindingAdapter</span><span style="color: #666600;" class="pun">({</span><span style="color: #880000;" class="str">"bind:imageUrl"</span><span style="color: #666600;" class="pun">,</span><span style="color: #880000;" class="str">"bind:error"</span><span style="color: #666600;" class="pun">})</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">static</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> loadImage</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">ImageView</span><span style="color: #000000;" class="pln"> view</span><span style="color: #666600;" class="pun">,</span><span style="color: #660066;" class="typ">String</span><span style="color: #000000;" class="pln"> url</span><span style="color: #666600;" class="pun">,</span><span style="color: #660066;" class="typ">Drawable</span><span style="color: #000000;" class="pln"> error</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                           </span><span style="color: #660066;" class="typ">Picasso</span><span style="color: #666600;" class="pun">.</span><span style="color: #000088;" class="kwd">with</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">view</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">getContext</span><span style="color: #666600;" class="pun">()).</span><span style="color: #000000;" class="pln">load</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">url</span><span style="color: #666600;" class="pun">).</span><span style="color: #000000;" class="pln">error</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">error</span><span style="color: #666600;" class="pun">).</span><span style="color: #000088;" class="kwd">into</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">view</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #666600;" class="pun">}</span>


                        1
                        2
                        <span style="color: #000088;" class="tag"><ImageView</span><span style="color: #882288;" class="atn">app:imageUrl</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{venue.imageUrl}"</span><span style="color: #000000;" class="pln">
                        </span><span style="color: #882288;" class="atn">app:error</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{@drawable/venueError}"</span><span style="color: #000088;" class="tag">/></span>


                        This adapter will be called if both imageUrl and error are used for an ImageView and imageUrl is a string and error is a drawable.



                        • Custom namespaces are ignored during matching.
                        • You can also write adapters for android namespace.

                          Binding adapter methods may optionally take the old values in their handlers. A method taking old and new values should have all old values for the attributes come first, followed by the new values:


                          1
                          2
                          3
                          4
                          5
                          6
                          7
                          8
                          9
                          <span style="color: #006666;" class="lit">@BindingAdapter</span><span style="color: #666600;" class="pun">(</span><span style="color: #880000;" class="str">"android:paddingLeft"</span><span style="color: #666600;" class="pun">)</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">static</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> setPaddingLeft</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">View</span><span style="color: #000000;" class="pln"> view</span><span style="color: #666600;" class="pun">,</span><span style="color: #000088;" class="kwd">int</span><span style="color: #000000;" class="pln"> oldPadding</span><span style="color: #666600;" class="pun">,</span><span style="color: #000088;" class="kwd">int</span><span style="color: #000000;" class="pln"> newPadding</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                             </span><span style="color: #000088;" class="kwd">if</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">oldPadding </span><span style="color: #666600;" class="pun">!=</span><span style="color: #000000;" class="pln"> newPadding</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                                 view</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">setPadding</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">newPadding</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln">
                                                 view</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">getPaddingTop</span><span style="color: #666600;" class="pun">(),</span><span style="color: #000000;" class="pln">
                                                 view</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">getPaddingRight</span><span style="color: #666600;" class="pun">(),</span><span style="color: #000000;" class="pln">
                                                 view</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">getPaddingBottom</span><span style="color: #666600;" class="pun">());</span><span style="color: #000000;" class="pln">
                             </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #666600;" class="pun">}</span>


                          Event handlers may only be used with interfaces or abstract classes with one abstract method. For example:


                          1
                          2
                          3
                          4
                          5
                          6
                          7
                          8
                          9
                          10
                          11
                          12
                          <span style="color: #006666;" class="lit">@BindingAdapter</span><span style="color: #666600;" class="pun">(</span><span style="color: #880000;" class="str">"android:onLayoutChange"</span><span style="color: #666600;" class="pun">)</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">static</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> setOnLayoutChangeListener</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">View</span><span style="color: #000000;" class="pln"> view</span><span style="color: #666600;" class="pun">,</span><span style="color: #660066;" class="typ">View</span><span style="color: #666600;" class="pun">.</span><span style="color: #660066;" class="typ">OnLayoutChangeListener</span><span style="color: #000000;" class="pln"> oldValue</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln">
                                 </span><span style="color: #660066;" class="typ">View</span><span style="color: #666600;" class="pun">.</span><span style="color: #660066;" class="typ">OnLayoutChangeListener</span><span style="color: #000000;" class="pln"> newValue</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                              </span><span style="color: #000088;" class="kwd">if</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">Build</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">VERSION</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">SDK_INT </span><span style="color: #666600;" class="pun">>=</span><span style="color: #660066;" class="typ">Build</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">VERSION_CODES</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">HONEYCOMB</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                                  </span><span style="color: #000088;" class="kwd">if</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">oldValue </span><span style="color: #666600;" class="pun">!=</span><span style="color: #000088;" class="kwd">null</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                                      view</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">removeOnLayoutChangeListener</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">oldValue</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                                  </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                                  </span><span style="color: #000088;" class="kwd">if</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">newValue </span><span style="color: #666600;" class="pun">!=</span><span style="color: #000088;" class="kwd">null</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                                      view</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">addOnLayoutChangeListener</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">newValue</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                                  </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                              </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #666600;" class="pun">}</span>


                          When a listener has multiple methods, it must be split into multiple listeners. For example, has two methods: and. We must then create two interfaces to differentiate the attributes and handlers for them.


                          1
                          2
                          3
                          4
                          5
                          6
                          7
                          8
                          <span style="color: #006666;" class="lit">@TargetApi</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">VERSION_CODES</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">HONEYCOMB_MR1</span><span style="color: #666600;" class="pun">)</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">interface</span><span style="color: #660066;" class="typ">OnViewDetachedFromWindow</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                              </span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> onViewDetachedFromWindow</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">View</span><span style="color: #000000;" class="pln"> v</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #006666;" class="lit">@TargetApi</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">VERSION_CODES</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">HONEYCOMB_MR1</span><span style="color: #666600;" class="pun">)</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">interface</span><span style="color: #660066;" class="typ">OnViewAttachedToWindow</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                              </span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> onViewAttachedToWindow</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">View</span><span style="color: #000000;" class="pln"> v</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #666600;" class="pun">}</span>


                          Because changing one listener will also affect the other, we must have three different binding adapters, one for each attribute and one for both, should they both be set.


                          1
                          2
                          3
                          4
                          5
                          6
                          7
                          8
                          9
                          10
                          11
                          12
                          13
                          14
                          15
                          16
                          17
                          18
                          19
                          20
                          21
                          22
                          23
                          24
                          25
                          26
                          27
                          28
                          29
                          30
                          31
                          32
                          33
                          34
                          35
                          36
                          37
                          38
                          39
                          40
                          41
                          <span style="color: #006666;" class="lit">@BindingAdapter</span><span style="color: #666600;" class="pun">(</span><span style="color: #880000;" class="str">"android:onViewAttachedToWindow"</span><span style="color: #666600;" class="pun">)</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">static</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> setListener</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">View</span><span style="color: #000000;" class="pln"> view</span><span style="color: #666600;" class="pun">,</span><span style="color: #660066;" class="typ">OnViewAttachedToWindow</span><span style="color: #000000;" class="pln"> attached</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                              setListener</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">view</span><span style="color: #666600;" class="pun">,</span><span style="color: #000088;" class="kwd">null</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln"> attached</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #006666;" class="lit">@BindingAdapter</span><span style="color: #666600;" class="pun">(</span><span style="color: #880000;" class="str">"android:onViewDetachedFromWindow"</span><span style="color: #666600;" class="pun">)</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">static</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> setListener</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">View</span><span style="color: #000000;" class="pln"> view</span><span style="color: #666600;" class="pun">,</span><span style="color: #660066;" class="typ">OnViewDetachedFromWindow</span><span style="color: #000000;" class="pln"> detached</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                              setListener</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">view</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln"> detached</span><span style="color: #666600;" class="pun">,</span><span style="color: #000088;" class="kwd">null</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #006666;" class="lit">@BindingAdapter</span><span style="color: #666600;" class="pun">({</span><span style="color: #880000;" class="str">"android:onViewDetachedFromWindow"</span><span style="color: #666600;" class="pun">,</span><span style="color: #880000;" class="str">"android:onViewAttachedToWindow"</span><span style="color: #666600;" class="pun">})</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">static</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> setListener</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">View</span><span style="color: #000000;" class="pln"> view</span><span style="color: #666600;" class="pun">,</span><span style="color: #000088;" class="kwd">final</span><span style="color: #660066;" class="typ">OnViewDetachedFromWindow</span><span style="color: #000000;" class="pln"> detach</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln">
                                  </span><span style="color: #000088;" class="kwd">final</span><span style="color: #660066;" class="typ">OnViewAttachedToWindow</span><span style="color: #000000;" class="pln"> attach</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                              </span><span style="color: #000088;" class="kwd">if</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">VERSION</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">SDK_INT </span><span style="color: #666600;" class="pun">>=</span><span style="color: #000000;" class="pln"> VERSION_CODES</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">HONEYCOMB_MR1</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                                  </span><span style="color: #000088;" class="kwd">final</span><span style="color: #660066;" class="typ">OnAttachStateChangeListener</span><span style="color: #000000;" class="pln"> newListener</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                                  </span><span style="color: #000088;" class="kwd">if</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">detach </span><span style="color: #666600;" class="pun">==</span><span style="color: #000088;" class="kwd">null</span><span style="color: #666600;" class="pun">&&</span><span style="color: #000000;" class="pln"> attach </span><span style="color: #666600;" class="pun">==</span><span style="color: #000088;" class="kwd">null</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                                      newListener </span><span style="color: #666600;" class="pun">=</span><span style="color: #000088;" class="kwd">null</span><span style="color: #666600;" class="pun">;</span><span style="color: #000000;" class="pln">
                                  </span><span style="color: #666600;" class="pun">}</span><span style="color: #000088;" class="kwd">else</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                                      newListener </span><span style="color: #666600;" class="pun">=</span><span style="color: #000088;" class="kwd">new</span><span style="color: #660066;" class="typ">OnAttachStateChangeListener</span><span style="color: #666600;" class="pun">()</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                                          </span><span style="color: #006666;" class="lit">@Override</span><span style="color: #000000;" class="pln">
                                          </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> onViewAttachedToWindow</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">View</span><span style="color: #000000;" class="pln"> v</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                                              </span><span style="color: #000088;" class="kwd">if</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">attach </span><span style="color: #666600;" class="pun">!=</span><span style="color: #000088;" class="kwd">null</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                                                  attach</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">onViewAttachedToWindow</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">v</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                                              </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                                          </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                                          </span><span style="color: #006666;" class="lit">@Override</span><span style="color: #000000;" class="pln">
                                          </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">void</span><span style="color: #000000;" class="pln"> onViewDetachedFromWindow</span><span style="color: #666600;" class="pun">(</span><span style="color: #660066;" class="typ">View</span><span style="color: #000000;" class="pln"> v</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                                              </span><span style="color: #000088;" class="kwd">if</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">detach </span><span style="color: #666600;" class="pun">!=</span><span style="color: #000088;" class="kwd">null</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                                                  detach</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">onViewDetachedFromWindow</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">v</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                                              </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                                          </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                                      </span><span style="color: #666600;" class="pun">};</span><span style="color: #000000;" class="pln">
                                  </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                                  </span><span style="color: #000088;" class="kwd">final</span><span style="color: #660066;" class="typ">OnAttachStateChangeListener</span><span style="color: #000000;" class="pln"> oldListener </span><span style="color: #666600;" class="pun">=</span><span style="color: #660066;" class="typ">ListenerUtil</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">trackListener</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">view</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln">
                                          newListener</span><span style="color: #666600;" class="pun">,</span><span style="color: #000000;" class="pln"> R</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">id</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">onAttachStateChangeListener</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                                  </span><span style="color: #000088;" class="kwd">if</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">oldListener </span><span style="color: #666600;" class="pun">!=</span><span style="color: #000088;" class="kwd">null</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                                      view</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">removeOnAttachStateChangeListener</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">oldListener</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                                  </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                                  </span><span style="color: #000088;" class="kwd">if</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">newListener </span><span style="color: #666600;" class="pun">!=</span><span style="color: #000088;" class="kwd">null</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                                      view</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">addOnAttachStateChangeListener</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">newListener</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                                  </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                              </span><span style="color: #666600;" class="pun">}</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #666600;" class="pun">}</span>


                          The above example is slightly more complicated than normal because View uses add and remove for the listener instead of a set method for . The android.databinding.adapters.ListenerUtil class helps keep track of the previous listeners so that they may be removed in the Binding Adaper.


                          By annotating the interfaces OnViewDetachedFromWindow and OnViewAttachedToWindow with@TargetApi(VERSION_CODES.HONEYCOMB_MR1), the data binding code generator knows that the listener should only be generated when running on Honeycomb MR1 and new devices, the same version supported by.


                          Converters




                          Object Conversions


                          When an Object is returned from a binding expression, a setter will be chosen from the automatic, renamed, and custom setters. The Object will be cast to a parameter type of the chosen setter.


                          This is a convenience for those using ObservableMaps to hold data. for example:


                          1
                          2
                          3
                          4
                          <span style="color: #000088;" class="tag"><TextView</span><span style="color: #000000;" class="pln">
                             </span><span style="color: #882288;" class="atn">android:text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">'@{userMap["lastName"]}'</span><span style="color: #000000;" class="pln">
                             </span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                             </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000088;" class="tag">/></span>


                          The userMap returns an Object and that Object will be automatically cast to parameter type found in the setter setText(CharSequence). When there may be confusion about the parameter type, the developer will need to cast in the expression.


                          Custom Conversions


                          Sometimes conversions should be automatic between specific types. For example, when setting the background:


                          1
                          2
                          3
                          4
                          <span style="color: #000088;" class="tag"><View</span><span style="color: #000000;" class="pln">
                             </span><span style="color: #882288;" class="atn">android:background</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{isError ? @color/red : @color/white}"</span><span style="color: #000000;" class="pln">
                             </span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                             </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000088;" class="tag">/></span>


                          Here, the background takes a Drawable, but the color is an integer. Whenever a Drawable is expected and an integer is returned, the int should be converted to a ColorDrawable. This conversion is done using a static method with a BindingConversion annotation:


                          1
                          2
                          3
                          4
                          <span style="color: #006666;" class="lit">@BindingConversion</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #000088;" class="kwd">public</span><span style="color: #000088;" class="kwd">static</span><span style="color: #660066;" class="typ">ColorDrawable</span><span style="color: #000000;" class="pln"> convertColorToDrawable</span><span style="color: #666600;" class="pun">(</span><span style="color: #000088;" class="kwd">int</span><span style="color: #000000;" class="pln"> color</span><span style="color: #666600;" class="pun">)</span><span style="color: #666600;" class="pun">{</span><span style="color: #000000;" class="pln">
                             </span><span style="color: #000088;" class="kwd">return</span><span style="color: #000088;" class="kwd">new</span><span style="color: #660066;" class="typ">ColorDrawable</span><span style="color: #666600;" class="pun">(</span><span style="color: #000000;" class="pln">color</span><span style="color: #666600;" class="pun">);</span><span style="color: #000000;" class="pln">
                          </span><span style="color: #666600;" class="pun">}</span>


                          Note that conversions only happen at the setter level, so it is not allowed to mix types like this:


                          1
                          2
                          3
                          4
                          <span style="color: #000088;" class="tag"><View</span><span style="color: #000000;" class="pln">
                             </span><span style="color: #882288;" class="atn">android:background</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{isError ? @drawable/error : @color/white}"</span><span style="color: #000000;" class="pln">
                             </span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                             </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000088;" class="tag">/></span>


                          Android Studio Support for Data Binding


                          Android Studio supports many of the code editing features for data binding code. For example, it supports the following features for data binding expressions:



                          • Syntax highlighting
                          • Flagging of expression language syntax errors
                          • XML code completion
                          • References, including (such as navigate to a declaration) and


                            Note: Arrays and a , such as the class, might display errors when there are no errors.


                            The Preview pane displays default values for data binding expressions if provided. In the following example excerpt of an element from a layout XML file, the Preview pane displays the PLACEHOLDER default text value in the TextView.


                            1
                            2
                            3
                            <span style="color: #000088;" class="tag"><TextView</span><span style="color: #882288;" class="atn">android:layout_width</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #882288;" class="atn">android:layout_height</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"wrap_content"</span><span style="color: #000000;" class="pln">
                               </span><span style="color: #882288;" class="atn">android:text</span><span style="color: #666600;" class="pun">=</span><span style="color: #880000;" class="atv">"@{user.firstName, default=PLACEHOLDER}"</span><span style="color: #000088;" class="tag">/></span>


                            If you need to display a default value during the design phase of your project, you can also use tools attributes instead of default expression values, as described in .

                  坚持原创及高品质技术分享,您的支持将鼓励我继续创作!