鍦� Android 寮�鍙戣繃绋嬩腑锛岀敱浜� Android 浣滀负 View 鎻忚堪鐨� xml 瑙嗗浘鍔熻兘杈冨急锛屽紑鍙戜腑寰堝鏄撳啓鍑鸿噧鑲跨箒鏉傜殑 Activity/Fragment锛岀敋鑷虫湁鍐欏嚭杩囨暟鍗冭浠g爜鐨� Activity銆傚ぇ閲忕殑鏄剧ず銆佹牎楠屻�佷簨浠跺搷搴斻�佸洖璋冩帴鍙e厖鏂ュ湪 Activity 涓�侫ctivity/Fragment 瀹為檯涓婃垚浜� View 鍜� Controller 鐨勬贩鍚堜綋锛屾棦瑕佹壙鎷� View 鐨勬樉绀哄姛鑳斤紝鍙堣鎵挎媴 Controller 鐨勬帶鍒跺姛鑳姐�傛壙鎷呯殑鍔熻兘杩囧锛岃啫鑳�鎴愮箒鑺滅殑宸ㄧ被涔熷氨涓嶈冻涓烘�簡銆�
UI 鐣岄潰璁捐妯″紡
鍦ㄤ紶缁熺殑 UI 璁捐妯″紡涓紝MVC 宸茬粡瀹炶返涓瘉鏄庝簡鍏朵环鍊硷紝骞跺湪婕暱鐨勪娇鐢ㄨ繃绋嬩腑锛堬紙MVC鏈�鏃╁嚭鐜颁簬 1970 骞翠唬锛夋紨鍖栧嚭 MVP 鍜� MVVM 澶氫釜鍙樼銆�
MVC
MVC 鎴戜滑閮界煡閬撴槸 Model-View-Controller锛屼负浜嗕娇寰楃▼搴忕殑鍚勪釜閮ㄥ垎鍒嗙闄嶄綆鑰﹀悎鎬э紝MVC 闄や簡鎶婂簲鐢ㄧ▼搴忓垎鎴� View銆丮odel 灞傦紝杩橀澶栫殑鍔犱簡涓�涓� Controller 灞傦紝瀹冪殑鑱岃矗涓鸿繘琛� Model 鍜� View 涔嬮棿鐨勫崗浣滐紙璺敱銆佽緭鍏ラ澶勭悊绛夛級鐨勫簲鐢ㄩ�昏緫锛汳odel 杩涜澶勭悊涓氬姟閫昏緫銆�
MVC 閫氬父鐨勫鐞嗘椂搴忓涓嬶細
- View 鎺ュ彈鐢ㄦ埛鐨勪氦浜掕姹傦紱
- View 灏嗚姹傝浆浜ょ粰 Controller锛�
- Controller 鎿嶄綔 Model 杩涜鏁版嵁鏇存柊锛�
- 鏁版嵁鏇存柊涔嬪悗锛孧odel 閫氱煡 View 鏁版嵁鍙樺寲锛�
- View 鏄剧ず鏇存柊涔嬪悗鐨勬暟鎹��
閫氬父 Model 浣跨敤 Observer 妯″紡閫氱煡 View 鏁版嵁鍙樺寲锛�
MVC 鐨勪紭鐐癸細
- 鎶婁笟鍔¢�昏緫鍜屽睍绀洪�昏緫鍒嗙锛屾ā鍧楀寲绋嬪害楂樸�備笖褰撳簲鐢ㄩ�昏緫闇�瑕佸彉鏇寸殑鏃跺�欙紝涓嶉渶瑕佸彉鏇翠笟鍔¢�昏緫鍜屽睍绀洪�昏緫锛屽彧闇�瑕丆ontroller鎹㈡垚鍙﹀涓�涓狢ontroller灏辫浜嗭紙Swappable Controller锛夈��
- 瑙傚療鑰呮ā寮忓彲浠ュ仛鍒板瑙嗗浘鍚屾椂鏇存柊銆�
MVD 鐨勭己鐐癸細
- Controller 娴嬭瘯鍥伴毦銆傚洜涓鸿鍥惧悓姝ユ搷浣滄槸鐢� View 鑷繁鎵ц锛岃�� View 鍙兘鍦� UI 鐜涓嬭繍琛屻�傚湪娌℃湁 UI 鐜涓嬪 Controller 杩涜鍗曞厓娴嬭瘯鐨勬椂鍊欙紝搴旂敤閫昏緫姝g‘鎬ф槸鏃犳硶楠岃瘉鐨勶細Model 鏇存柊鐨勬椂鍊欙紝鏃犳硶瀵� View 鐨勬洿鏂版搷浣滆繘琛屾柇瑷�銆�
- View 鏃犳硶缁勪欢鍖栥�俈iew 鏄己渚濊禆鐗瑰畾鐨� Model 鐨勶紝濡傛灉闇�瑕佹妸杩欎釜 View 鎶藉嚭鏉ヤ綔涓轰竴涓彟澶栦竴涓簲鐢ㄧ▼搴忓彲澶嶇敤鐨勭粍浠跺氨鍥伴毦浜嗐�傚洜涓轰笉鍚岀▼搴忕殑鐨� Model 鏄笉涓�鏍风殑銆�
- 褰撴湁鍙樺寲鐨勬椂鍊欓渶瑕佸悓鏃剁淮鎶� Model, View, Controller 鍙婂叾浜や簰锛岃繖鏄剧劧璁╀簨鎯呭鏉傚寲浜嗐��
MVP
涓轰簡瑙e喅 MVC 鐨勬潈闄愶紝MVP 瀵� MVC 杩涜浜嗘敼鑹紝MVP 妯″紡鎶� MVC 妯″紡涓殑 Controller 鎹㈡垚浜� Presenter锛�
MVP 閫氬父鐨勮皟鐢ㄦ椂搴忓涓嬶細
- View 鎺ュ彈鐢ㄦ埛鐨勪氦浜掕姹傦紱
- View 灏嗚姹傝浆浜ょ粰 Presenter锛�
- Presenter 鎿嶄綔 Model 杩涜涓氬姟澶勭悊锛�
- Model 閫氱煡 Presenter 鏁版嵁鍙戠敓鍙樺寲锛�
- Presenter 鏇存柊 View 鐨勬暟鎹��
鍜� MVC 涓嶅悓鐨勬槸锛孭resenter 浼氬弽浣滅敤浜� View锛屼笉鍍� Controller 鍙兘琚姩鐨勬帴鍙� View 鐨勬寚鎸ャ��
閫氬父鎴戜滑浼氭娊璞� View 鎺ュ彛锛屾毚闇插睘鎬у拰浜嬩欢锛岀劧鍚� Presenter 寮曠敤 View 鎺ュ彛銆傝繖鏍峰彲浠ュ緢瀹规槗鐨勬瀯閫� View 鐨� Mock 瀵硅薄锛屾彁楂樺彲鍗曞厓娴嬭瘯鎬с�傚湪杩欓噷锛孭resenter 鐨勮矗浠诲彉澶т簡锛屼笉浠呰鎿嶄綔鏁版嵁锛岃�屼笖瑕佹洿鏂� View銆�
涓婇潰璁茬殑鏄� MVP 鐨� Passive View 妯″紡锛岃妯″紡涓� View 闈炲父 Passive锛屽畠鍑犱箮浠�涔堥兘涓嶇煡閬擄紝Presenter 璁╁畠骞蹭粈涔堝畠灏卞共浠�涔堛��
鍦ㄥ疄闄呯殑瀹炵幇涓紝鏈変汉浼氬�惧悜浜� 濂� Presenter 涓�閮ㄥ垎绠�鍗曠殑鍚屾閫昏緫浜ょ粰 View 鑷繁鍘诲仛锛孭resenter 鍙礋璐f瘮杈冨鏉傜殑銆侀珮灞傛鐨� UI 鎿嶄綔锛屾墍浠ュ彲浠ユ妸瀹冪湅鎴愪竴涓� Supervising Controller锛岃繖绉嶆ā寮忎篃琚О涓� The Supervising Controller MVP锛�
MVP 鐨勪紭鐐癸細
- 渚夸簬娴嬭瘯銆侾resenter 瀵� View 鏄�氳繃鎺ュ彛杩涜锛屽湪瀵� Presenter 杩涜涓嶄緷璧� UI 鐜鐨勫崟鍏冩祴璇曠殑鏃跺�欍�傚彲浠ラ�氳繃 Mock 涓�涓� View 瀵硅薄锛岃繖涓璞″彧闇�瑕佸疄鐜颁簡 View 鐨勬帴鍙e嵆鍙��
- View 鍙互杩涜缁勪欢鍖栥�傚湪 MVP 褰撲腑锛孷iew 涓嶄緷璧� Model銆傝繖鏍峰氨鍙互璁� View 浠庣壒瀹氱殑涓氬姟鍦烘櫙涓劚绂诲嚭鏉ワ紝鍙互璇� View 鍙互鍋氬埌瀵逛笟鍔″畬鍏ㄦ棤鐭ャ�傚畠鍙渶瑕佹彁渚涗竴绯诲垪鎺ュ彛鎻愪緵缁欎笂灞傛搷浣溿�傝繖鏍峰氨鍙互鍋氬埌楂樺害鍙鐢ㄧ殑 View缁勪欢銆�
MVP 缂虹偣锛�
- Presenter 涓櫎浜嗗簲鐢ㄩ�昏緫浠ュ锛岃繕鏈夊ぇ閲忕殑 View->Model锛孧odel->View 鐨勬墜鍔ㄥ悓姝ラ�昏緫锛岄�犳垚 Presenter 姣旇緝绗ㄩ噸锛岀淮鎶よ捣鏉ヤ細姣旇緝鍥伴毦銆�
MVVM
MVVM 鍙互鐪嬩綔鏄竴绉嶇壒娈婄殑 MVP锛圥assive View锛夋ā寮忥紝鎴栬�呰鏄 MVP 妯″紡鐨勮繘涓�姝ユ敼鑹��
MVVM 妯″紡鏈�鏃╂槸寰蒋鍏徃鎻愬嚭锛屽苟涓斾簡澶ч噺浣跨敤鍦�.NET鐨刉PF鍜孲liverlight涓��2005骞村井杞伐绋嬪笀John Gossman鍦ㄨ嚜宸辩殑鍗氬涓婇娆″叕甯冧簡MVVM妯″紡銆�
MVVM 浠h〃鐨勬槸 Model-View-ViewModel銆侻VVM 妯″紡灏� Presenter 鏀瑰悕涓� ViewModel锛屽熀鏈笂涓� MVP 妯″紡瀹屽叏涓�鑷达紝鍞竴鍖哄埆鍦ㄤ簬 ViewModel 灏嗗瘑鍒囧叧鑱旂殑 Model 鍜� View 鐨勯�昏緫鍗曠嫭鎻愬彇鍑烘潵锛岀敤鏁版嵁缁戝畾灏嗕粬浠叧鑱斿埌涓�璧枫�侻odel 鐨勬敼鍙樹細閫氳繃 ViewModel 鏉ユ槧灏勫埌 View 涓婏紝鍙嶄箣浜︾劧銆傛暟鎹粦瀹氫綘鍙互璁や负鏄� Observer 妯″紡鎴栬�呮槸 Publish/Subscribe 妯″紡锛屽師鐞嗛兘鏄负浜嗙敤涓�绉嶇粺涓�鐨勯泦涓殑鏂瑰紡瀹炵幇棰戠箒闇�瑕佽瀹炵幇鐨勬暟鎹洿鏂伴棶棰樸��
姣旇捣MVP锛孧VVM 涓嶄粎绠�鍖栦簡涓氬姟涓庣晫闈㈢殑渚濊禆鍏崇郴锛岃繕浼樺寲浜嗘暟鎹绻佹洿鏂扮殑瑙e喅鏂规锛岀敋鑷冲彲浠ヨ鎻愪緵浜嗕竴绉嶆湁鏁堢殑瑙e喅妯″紡銆�
MVVM 浼樼偣锛�
- 鐪佸幓浜唌odel鍙樺寲涔嬪悗鎵嬪姩淇敼view鍜寁iew鍙樺寲涔嬪悗鎵嬪姩淇敼model鐨勭箒鐞愬伐浣滐紱
- UI鍜屽姛鑳芥洿鍔犳澗鑰﹀悎浜嗭紝鍔熻兘鐨勫彲娴嬭瘯鎬у氨瓒婃潵瓒婂己銆�
MVVM 缂虹偣锛�
- 鍦ㄥ鏉傜殑鎯呭喌涓嬶紝寰堥毦棰勫厛璁捐濂借冻澶熼�氱敤鐨� ViewModel锛�
- 閫氬父浼氫緷璧栦簬鐗瑰畾鐨勬暟鎹粦瀹氭鏋讹紱
Android Data Binding
2015 Google IO 澶т細甯︽潵鐨� Data Binding 搴撲娇寰� Android 寮�鍙戣�呭彲浠ユ柟渚跨殑瀹炵幇 MVVM 鏋舵瀯妯″紡銆�
璀﹀憡锛欴ata Binding 搴撶洰鍓嶈繕鏄� Beta 鐗堟湰锛岄噰鐢ㄩ渶璋ㄦ厧銆�
浠ヤ笅澶ч儴鍒嗗唴瀹规憳褰曡嚜Data Binding锛堟暟鎹粦瀹氾級鐢ㄦ埛鎸囧崡锛屾牴鎹� Android 瀹樻柟鑻辨枃鎸囧崡 鍋氫簡涓�浜涙洿鏂般��
閰嶇疆鐜
鏈�鏂扮増鐨� Android Studio 宸茬粡鍐呯疆浜嗗 Android Data Binding 妗嗘灦鐨勬敮鎸侊紝閰嶇疆璧锋潵涔熷緢绠�鍗曪紝鍙渶瑕佸湪 app 鐨� build.gradle 鏂囦欢涓坊鍔犱笅闈㈢殑鍐呭灏卞ソ浜�
android {
....
dataBinding {
enabled = true
}
}
Data Binding Layout 鏂囦欢
Data Binding 琛ㄨ揪寮�
Data Binding layout 鏂囦欢鏈夌偣涓嶅悓鐨勬槸锛氳捣濮嬫牴鏍囩鏄� layout锛屾帴涓嬫潵涓�涓� data 鍏冪礌浠ュ強涓�涓� view 鐨勬牴鍏冪礌銆傝繖涓� view 鍏冪礌灏辨槸浣犳病鏈変娇鐢� Data Binding鐨刲ayout鏂囦欢鐨勬牴鍏冪礌銆備妇渚嬭鏄庡涓嬶細
鍦╠ata鍐呮弿杩颁簡涓�涓悕涓簎ser鐨勫彉閲忓睘鎬э紝浣垮叾鍙互鍦ㄨ繖涓猯ayout涓娇鐢細
鍦╨ayout鐨勫睘鎬ц〃杈惧紡鍐欎綔 @{}锛屼笅闈㈡槸涓�涓� TextView 鐨� text 璁剧疆涓� user 鐨� firstName 灞炴�э細
Data Object
鍋囪浣犳湁涓�涓猽ser鐨刾lain-old Java Object锛圥OJO锛夛細
public class User {
public final String firstName;
public final String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
杩欎釜绫诲瀷鐨勫璞℃嫢鏈変粠涓嶆敼鍙樼殑鏁版嵁銆傚湪 app 涓畠鏄父瑙佺殑锛屽彲浠ヨ鍙栦竴娆″苟涓斾箣鍚庝粠涓嶆敼鍙樸�傚綋鐒朵篃鍙互浣跨敤 JavaBeans 瀵硅薄锛�
public class User {
private final String firstName;
private final String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
}
浠� Data Binding 鐨勮搴︽潵鐪嬶紝杩欎袱涓被鏄瓑浠风殑銆傜敤浜� TextView 涓殑 android:text 灞炴�х殑琛ㄨ揪寮� @{user.firstName} 灏嗚闂墠鑰� POJO 瀵硅薄涓殑 firstName 鍜屽悗鑰� JavaBeans 瀵硅薄涓殑 getFirstName() 鏂规硶銆�
缁戝畾鏁版嵁
榛樿鎯呭喌涓嬶紝涓�涓� Binding 绫讳細鍩轰簬 layout 鏂囦欢鐨勫悕绉拌�屼骇鐢燂紝灏嗗叾杞崲涓� Pascal case锛堣瘧娉細棣栧瓧姣嶅ぇ鍐欑殑鍛藉悕瑙勮寖锛夊苟涓旀坊鍔� 鈥淏inding鈥� 鍚庣紑銆備笂杩扮殑 layout 鏂囦欢鏄� activity_main.xml锛屽洜姝ょ敓鎴愮殑绫诲悕鏄� ActivityMainBinding銆傛绫诲寘鍚粠 layout 灞炴�у埌 layout 鐨� Views 涓墍鏈夌殑 bindings锛堜緥濡倁ser鍙橀噺锛夛紝骞朵笖瀹冭繕鐭ラ亾濡備綍缁� Binding 琛ㄨ揪寮忓垎閰嶆暟鍊笺�傚垱寤� bindings 鐨勬渶绠�鍗曠殑鏂瑰紡鏄湪 inflating锛堣瘧娉細layout鏂囦欢涓嶢ctivity/Fragment鐨勨�滈摼鎺モ�濓級鏈熼棿濡備笅锛�
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
User user = new User("Test", "User");
binding.setUser(user);
}
灏辨槸杩欐牱锛岃繍琛� app 鍚庯紝浣犲皢浼氱湅鍒� Test User銆傛垨鑰呬綘鍙互閫氳繃濡備笅鑾峰彇 View锛�
MainActivityBinding binding = MainActivityBinding.inflate(getLayoutInflater());
濡傛灉浣犲湪 ListView 鎴栬�� RecyclerView adapter 浣跨敤 Data Binding 鏃讹紝浣犲彲鑳戒細浣跨敤锛�
ListItemBinding binding = ListItemBinding.inflate(layoutInflater, viewGroup,
false);
//or
ListItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false);
缁戝畾浜嬩欢
灏卞儚浣犲彲浠ュ湪xml鏂囦欢閲岄潰浣跨敤灞炴�ndroid:onClick缁戝畾Activity閲岄潰鐨勪竴涓柟娉曚竴鏍凤紝Data Binding Library 鎵╁睍浜嗘洿澶氱殑浜嬩欢鍙互鐢ㄦ潵缁戝畾鏂规硶锛屾瘮濡� View.OnLongClickListener 鏈変釜鏂规硶 onLongClick(), 浣犲氨鍙互浣跨敤 android:onLongClick 灞炴�ф潵缁戝畾涓�涓柟娉曪紝闇�瑕佹敞鎰忕殑鏄粦瀹氱殑鏂规硶鐨勭鍚嶅繀椤诲拰璇ュ睘鎬у師鏈搴旂殑鏂规硶鐨勭鍚嶅畬鍏ㄤ竴鏍凤紝鍚﹀垯缂栬瘧闃舵浼氭姤閿欍��
涓嬮潰涓句緥鏉ヨ鏄庡叿浣撴�庝箞浣跨敤锛屽厛鐪嬬敤鏉ョ粦瀹氫簨浠剁殑绫伙細
public class MyHandlers {
public void onClickButton(View view) { ... }
public void afterFirstNameChanged(Editable s) { ... }
}
鐒跺悗灏辨槸layout鏂囦欢锛�
娣卞叆 Layout 鏂囦欢
Import
闆朵釜鎴栧涓� import 鍏冪礌鍙兘鍦� data 鍏冪礌涓娇鐢ㄣ�傝繖浜涘彧鐢ㄥ湪浣犵殑 layout 鏂囦欢涓坊鍔犲紩鐢紝灏卞儚鍦� Java 涓細
鐜板湪锛孷iew 鍙互浣跨敤浣犵殑 Binding 琛ㄨ揪寮忥細
褰撶被鍚嶆湁鍐茬獊鏃讹紝鍏朵腑涓�涓被鍚嶅彲浠ラ噸鍛藉悕涓篴lias:锛�
杩欐牱锛屽湪璇� layout 鏂囦欢涓� Vista 瀵瑰簲 com.example.real.estate.View锛岃�孷iew瀵瑰簲android.view.View銆傚鍏ョ殑绫诲瀷鍙互鍦╒ariable鍜岃〃杈惧紡涓娇鐢ㄤ綔涓哄紩鐢ㄦ潵浣跨敤锛�
娉ㄦ剰锛欰ndroid Studio杩樻病鏈夊鐞唅mports锛屾墍浠ヨ嚜鍔ㄥ鍏ariable鍦ㄤ綘鐨処DE涓嶈兘浣跨敤銆傛偍鐨刟pp浠嶄細姝e父缂栬瘧锛屼綘鍙互鍦ㄦ偍鐨刅ariable瀹氫箟涓娇鐢ㄥ畬鍏ㄧ鍚堣瀹氱殑鍚嶇О鏉ヨВ鍐宠IDE闂銆�
瀵煎叆鐨勭被鍨嬭繕鍙互鍦ㄨ〃杈惧紡涓娇鐢� static 灞炴�у拰鏂规硶锛�
鈥�
灏卞儚鍦↗ava涓紝java.lang銆�*鏄嚜鍔ㄥ鍏ョ殑銆�
Variables
鍦╠ata涓彲浠ヤ娇鐢ㄤ换鎰忔暟閲忕殑variable鍏冪礌銆傛瘡涓�涓獀ariable鍏冪礌鎻忚堪浜嗕竴涓敤浜巐ayout鏂囦欢涓瑽inding琛ㄨ揪寮忕殑灞炴�с��
璇ariable绫诲瀷鍦ㄧ紪璇戞椂妫�鏌ワ紝鍥犳濡傛灉涓�涓猇ariable瀹炵幇浜哋bservable鎴杘bservable collection锛岃繖搴旇鍙嶆槧鍦ㄧ被鍨嬩腑銆傦紙璇戞敞锛氶渶瑕佹煡鎵捐祫鏂欐潵鐞嗚В锛夊鏋渧ariable鏄竴涓病鏈夊疄鐜癘bservable鎺ュ彛鐨勫熀鏈被鎴栬�呮帴鍙o紝Variables涓嶄細琚玱bserved锛�
褰撳浜庡绉嶉厤缃湁涓嶅悓鐨刲ayout鏂囦欢鏃讹紙濡傦紝妯悜鎴栫旱鍚戯級锛孷ariables浼氳鍚堝苟銆傝繖浜沴ayout鏂囦欢涔嬮棿蹇呴』涓嶈兘鏈夊啿绐佺殑Variable瀹氫箟銆�
浜х敓鐨凚inding绫诲浜庢瘡涓�涓弿杩扮殑Variables閮戒細鏈塻etter鍜実etter銆傝繖浜沄ariables浼氫娇鐢ㄩ粯璁ょ殑Java鍊� - null锛堝紩鐢ㄧ被鍨嬶級銆�0锛坕nt锛夈�乫alse锛坆oolean锛夌瓑绛夛紝鐩村埌璋冪敤setter鏃躲��
鑷畾涔� Binding 绫诲悕绉�
榛樿鎯呭喌涓嬶紝Binding绫荤殑鍛藉悕鏄熀浜庢墍杩發ayout鏂囦欢鐨勫悕绉帮紝鐢ㄥぇ鍐欏紑澶达紝闄ゅ幓涓嬪垝绾匡紙锛変互鍙婏紙锛夊悗鐨勭涓�涓瓧姣嶅ぇ鍐欙紝鐒跺悗娣诲姞鈥淏inding鈥濆悗缂�銆傝繖涓被灏嗚鏀剧疆鍦ㄤ竴涓ā鍧楀皝瑁呭寘閲岀殑databinding灏佽鍖呬笅銆備緥濡傦紝鎵�杩發ayout鏂囦欢contact_item.xml灏嗙敓鎴怌ontactItemBinding銆傚鏋滄ā鍧楀寘鏄痗om.example.my.app锛岄偅涔堝畠灏嗚鏀剧疆鍦╟om.example.my.app.databinding銆�
Binding绫诲彲閫氳繃璋冩暣data鍏冪礌涓殑class灞炴�ф潵閲嶅懡鍚嶆垨鏀剧疆鍦ㄤ笉鍚岀殑鍖呬腑銆備緥濡傦細
...
鍦ㄦā鍧楀皝瑁呭寘鐨刣atabinding鍖呬腑浼氱敓鎴愬悕涓篊ontactItem鐨凚inding绫汇�傚鏋滆鎯宠璇ョ被鐢熸垚鍦ㄤ笉鍚岀殑鍖呯锛屼綘闇�瑕佹坊鍔犲墠缂�.锛屽涓嬶細
...
鍦ㄨ繖涓儏鍐典笅锛孋ontactItem绫荤洿鎺ュ湪妯″潡鍖呯鐢熸垚銆傛垨鑰呬綘鍙互鎻愪緵鏁翠釜鍖呭悕锛�
...
Includes
閫氳繃浣跨敤application namespace浠ュ強鍦ㄥ睘鎬т腑鐨刅ariable鍚嶅瓧浠庡鍣╨ayout涓紶閫扸ariables鍒颁竴涓鍖呭惈鐨刲ayout锛�
娉ㄦ剰锛氬湪name.xml浠ュ強contact.xml涓や釜layout鏂囦欢涓繀闇�瑕佹湁user variable
Data binding 涓嶆敮鎸佸寘鍚� merge 鍏冪礌浣滀负鐩存帴鐨勫瓙鍏冪礌锛屾瘮濡備互涓媗ayout鏄笉鏀寔鐨勶細
琛ㄨ揪寮忚瑷�
甯哥敤琛ㄨ揪寮忚窡Java琛ㄨ揪寮忓緢鍍忥紝浠ヤ笅杩欎簺鏄竴鏍风殑锛�
- 鏁板琛ㄨ揪寮� + 鈥� / * %
- 瀛楃涓查摼鎺� +
- 閫昏緫鎿嶄綔绗� && ||
- 浜屽厓鎿嶄綔绗� & | ^
- 涓�鍏冩搷浣滅 + 鈥� ! ~
- Shift >> >>> <<
- 姣旇緝 == > < >= <=
- instanceof
- Grouping ()
- Literals 鈥� character, String, numeric, null
- Cast
- 鍑芥暟璋冪敤
- 鍊煎煙寮曠敤锛團ield access锛�
- 閫氳繃[]璁块棶鏁扮粍閲岄潰鐨勫璞�
- 涓夊厓鎿嶄綔绗� ?:
绀轰緥锛�
android:text="@{String.valueOf(index + 1)}"
android:visibility="@{age < 13 ? View.GONE : View.VISIBLE}"
android:transitionName='@{"image_" + id}'
缂哄皯鐨勬搷浣滐細
- this
- super
- new
- 鏄惧紡娉涘瀷璋冪敤
Null鍚堝苟鎿嶄綔
?? - 宸﹁竟鐨勫璞″鏋滃畠涓嶆槸null锛岄�夋嫨宸﹁竟鐨勫璞★紱鎴栬�呭鏋滃畠鏄痭ull锛岄�夋嫨鍙宠竟鐨勫璞★細
android:text="@{user.displayName ?? user.lastName}"
鍑芥暟涓婄殑鍐欐硶濡備笅锛�
android:text="@{user.displayName != null ? user.displayName : user.lastName}"
灞炴�у紩鐢�
鎴戜滑宸茬粡鍦ㄥ墠杈光�淒ata Binding琛ㄨ揪寮忊�濅腑鎻愬埌浜咼avaBean寮曠敤鐨勭畝鐭牸寮忋��
褰撲竴涓〃杈惧紡寮曠敤涓�涓被鐨勫睘鎬э紝瀹冧粛浣跨敤鍚屾牱鐨勬牸寮忓浜庡瓧娈点�乬etters浠ュ強ObservableFields銆�
android:text="@{user.lastName}"
閬垮厤 NullPointerException
Data Binding浠g爜鐢熸垚鏃惰嚜鍔ㄦ鏌ユ槸鍚︿负nulls鏉ラ伩鍏嶅嚭鐜皀ull pointer exceptions閿欒銆備緥濡傦紝鍦ㄨ〃杈惧紡@{user.name}涓紝濡傛灉user鏄痭ull锛�user.name浼氳祴浜堝畠鐨勯粯璁ゅ�硷紙null锛夈�傚鏋滀綘寮曠敤user.age锛宎ge鏄痠nt绫诲瀷锛岄偅涔堝畠鐨勯粯璁ゅ�兼槸0銆�
闆嗗悎
甯哥敤鐨勯泦鍚堬細arrays銆乴ists銆乻parse lists浠ュ強maps锛屼负浜嗙畝渚块兘鍙互浣跨敤[]鏉ヨ闂��
鈥�
android:text="@{list[index]}"
鈥�
android:text="@{sparse[index]}"
鈥�
android:text="@{map[key]}"
瀛楃涓�
褰撲娇鐢ㄥ崟寮曞彿鍖呭惈灞炴�у�兼椂锛屽湪琛ㄨ揪寮忎腑浣跨敤鍙屽紩鍙峰緢瀹规槗锛�
android:text='@{map["firstName"]}'
浣跨敤鍙屽紩鍙锋潵鍖呭惈灞炴�у�间篃鏄彲浠ョ殑銆傚瓧绗︿覆鍓嶅悗闇�瑕佷娇鐢�"`"锛�
android:text="@{map[`firstName`]}"
android:text="@{map["firstName"]}"
Resources
浣跨敤姝e父鐨勮〃杈惧紡鏉ヨ闂畆esources涔熸槸鍙鐨勶細
android:padding="@{large? @dimen/largePadding : @dimen/smallPadding}"
鏍煎紡鍖栧瓧绗︿覆鍜屽鏁板彲浠ラ�氳繃鎻愪緵鍙傛暟鏉ュ垽鏂�
android:text="@{@string/nameFormat(firstName, lastName)}"
android:text="@{@plurals/banana(bananaCount)}"
褰撳鏁伴渶瑕佸涓弬鏁版椂锛屾墍鏈夌殑鍙傛暟閮戒細閫氳繃锛�
Have an orange
Have %d oranges
android:text="@{@plurals/orange(orangeCount, orangeCount)}"
涓�浜涜祫婧愰渶瑕佹樉寮忕被鍨嬪垽鏂細
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 Object
浠讳綍Plain old Java object锛圥O鈥嬧�婮O锛夊彲鐢ㄤ簬Data Binding锛屼絾淇敼POJO涓嶄細瀵艰嚧UI鏇存柊銆侱ata Binding鐨勭湡姝h兘鍔涙槸褰撴暟鎹彉鍖栨椂锛屽彲浠ラ�氱煡缁欎綘鐨凞ata瀵硅薄銆傛湁涓夌涓嶅悓鐨勬暟鎹彉鍖栭�氱煡鏈哄埗锛歄bservable瀵硅薄銆丱bservableFields浠ュ強observable collections銆�
褰撹繖浜涘彲瑙傚療Data瀵硅薄鈥嬧�嬬粦瀹氬埌UI锛孌ata瀵硅薄灞炴�х殑鏇存敼鍚庯紝UI涔熷皢鑷姩鏇存柊銆�
Observable 瀵硅薄
瀹炵幇android.databinding.Observable鎺ュ彛鐨勭被鍙互鍏佽闄勫姞涓�涓洃鍚櫒鍒癇ound瀵硅薄浠ヤ究鐩戝惉瀵硅薄涓婄殑鎵�鏈夊睘鎬х殑鍙樺寲銆�
Observable鎺ュ彛鏈変竴涓満鍒舵潵娣诲姞鍜屽垹闄ょ洃鍚櫒锛屼絾閫氱煡涓庡惁鐢卞紑鍙戜汉鍛樼鐞嗐�備负浜嗕娇寮�鍙戞洿瀹规槗锛屼竴涓狟aseObservable鐨勫熀绫讳负瀹炵幇鐩戝惉鍣ㄦ敞鍐屾満鍒惰�屽垱寤恒�侱ata瀹炵幇绫讳緷鐒惰礋璐i�氱煡褰撳睘鎬ф敼鍙樻椂銆傝繖鏄�氳繃鎸囧畾涓�涓狟indable娉ㄨВ缁檊etter浠ュ強setter鍐呴�氱煡鏉ュ畬鎴愮殑銆�
private static class User extends BaseObservable {
private String firstName;
private String lastName;
@Bindable
public String getFirstName() {
return this.firstName;
}
@Bindable
public String getFirstName() {
return this.lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
notifyPropertyChanged(BR.firstName);
}
public void setLastName(String lastName) {
this.lastName = lastName;
notifyPropertyChanged(BR.lastName);
}
}
鍦ㄧ紪璇戞湡闂达紝Bindable娉ㄨВ鍦˙R绫绘枃浠朵腑鐢熸垚涓�涓狤ntry銆侭R绫绘枃浠朵細鍦ㄦā鍧楀寘鍐呯敓鎴愩�傚鏋滅敤浜嶥ata绫荤殑鍩虹被涓嶈兘鏀瑰彉锛孫bservable鎺ュ彛閫氳繃鏂逛究鐨凱ropertyChangeRegistry鏉ュ疄鐜扮敤浜庡偍瀛樺拰鏈夋晥鍦伴�氱煡鐩戝惉鍣ㄣ��
Observable 瀛楁
涓�浜涘皬宸ヤ綔浼氭秹鍙婂埌鍒涘缓Observable绫伙紝鍥犳閭d簺鎯宠鑺傜渷鏃堕棿鎴栬�呭嚑涔庢病鏈夊嚑涓睘鎬х殑寮�鍙戣�呭彲浠ヤ娇鐢∣bservableFields銆侽bservableFields鏄嚜鍖呭惈鍏锋湁鍗曚釜瀛楁鐨刼bservable瀵硅薄銆傚畠鏈夋墍鏈夊熀鏈被鍨嬪拰涓�涓槸寮曠敤绫诲瀷銆傝浣跨敤瀹冮渶瑕佸湪data瀵硅薄涓垱寤簆ublic final瀛楁锛�
private static class User extends BaseObservable {
public final ObservableField firstName =
new ObservableField<>();
public final ObservableField lastName =
new ObservableField<>();
public final ObservableInt age = new ObservableInt();
}
灏辨槸杩欐牱锛岃璁块棶璇ュ�硷紝浣跨敤set鍜実et鏂规硶锛�
user.firstName.set("Google");
int age = user.age.get();
鍦ㄥ疄璺佃繃绋嬩腑锛屾湁鏃� notifyPropertyChanged(BR.lastName); 寰堝鏄撳紩鐢ㄩ敊璇紝鍥犳锛屽紑鍙戣繃绋嬩腑杩樻槸鎺ㄨ崘浣跨敤 ObservableField銆�
Observable 闆嗗悎
涓�浜沘pp浣跨敤鏇村鐨勫姩鎬佺粨鏋勬潵淇濆瓨鏁版嵁銆侽bservable闆嗗悎鍏佽閿帶璁块棶杩欎簺data瀵硅薄銆侽bservableArrayMap鐢ㄤ簬閿槸寮曠敤绫诲瀷,濡係tring銆�
ObservableArrayMap user = new ObservableArrayMap<>();
user.put("firstName", "Google");
user.put("lastName", "Inc.");
user.put("age", 17);
鍦╨ayout鏂囦欢涓紝閫氳繃String閿彲浠ヨ闂甿ap锛�
鈥�
ObservableArrayList 鍦ㄦ暣褰㈤敭鍊煎緢鏈夌敤锛�
ObservableArrayList
鍦╨ayout鏂囦欢涓紝閫氳繃绱㈠紩鍙互璁块棶list锛�
鈥�
鐢熸垚 Binding
Binding绫荤殑鐢熸垚閾炬帴浜唋ayout涓璿ariables涓嶸iews銆傚鍓嶆墍杩帮紝Binding鐨勫悕绉板拰鍖呭悕鍙互瀹氬埗銆傛墍鐢熸垚鐨凚inding绫婚兘鎵╁睍浜哸ndroid.databinding.ViewDataBinding銆�
鍒涘缓
Binding搴斿湪inflation涔嬪悗灏辩珛椹垱寤猴紝浠ョ‘淇漋iew灞傛缁撴瀯涓嶅湪涔嬪墠鎵撴壈layout涓殑binding鍒皏iews涓婄殑琛ㄨ揪寮忋�傛湁鍑犱釜鏂规硶鍙互缁戝畾鍒颁竴涓猯ayout銆傛渶甯歌鐨勬槸鍦˙inding绫讳笂浣跨敤闈欐�佹柟娉�.inflate鏂规硶杞藉叆View鐨勫眰娆$粨鏋勫苟涓旂粦瀹氬埌瀹冨彧闇�杩欎竴姝ャ�傝繕鏈変竴涓洿绠�鍗曠殑鐗堟湰锛屽彧闇�瑕丩ayoutInflater杩樻湁涓�涓槸閲囩敤ViewGroup锛�
MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater);
MyLayoutBinding binding = MyLayoutBinding.inflate(LayoutInflater, viewGroup, false);
濡傛灉浣跨敤涓嶅悓鐨勬満鍒惰浇鍏ayout锛屼粬鍙竴鍒嗗紑缁戝畾锛�
MyLayoutBinding binding = MyLayoutBinding.bind(viewRoot);
鏈夋椂Binding涓嶈兘鎻愬墠鐭ラ亾锛屽浜庤繖绉嶆儏鍐碉紝鍙互浣跨敤DataBindingUtil绫绘潵鍒涘缓Binding锛�
ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater, layoutId,
parent, attachToParent);
ViewDataBinding binding = DataBindingUtil.bindTo(viewRoot, layoutId);
甯D鐨刅iews
鍦╨ayout涓浜庢瘡涓甫ID鐨刅iew浼氱敓鎴愪竴涓猵ublic final瀛楁銆侭inding鍦╒iew灞傛缁撴瀯涓婂仛鍗曚竴鐨勪紶閫掞紝鎻愬彇甯D鐨刅iews銆傝繖绉嶆満鍒舵瘮璧锋煇浜沄iews浣跨敤findViewById杩樿蹇�備緥濡�:
瀹冧細鐢熸垚濡備笅鐨凚inding绫伙細
public final TextView firstName;
public final TextView lastName;
IDs涓嶅儚娌℃湁Data Bindings閭f牱鍑犱箮娌℃湁蹇呰锛屼絾鏄粛鐒朵細鏈変竴浜涘疄渚嬮渶瑕佷粠浠g爜涓闂甐iews銆�
Variables
姣忎釜Variable浼氭湁璁块棶鏂规硶銆�
瀹冧細鍦˙inding涓敓鎴恠etters鍜実etters锛�
public abstract com.example.User getUser();
public abstract void setUser(com.example.User user);
public abstract Drawable getImage();
public abstract void setImage(Drawable image);
public abstract String getNote();
public abstract void setNote(String note);
Binding杩涢樁
鍔ㄦ�乂ariables
鏈夋椂锛屼笉鐭ラ亾鍏蜂綋鐨凚inding绫伙紝渚嬪锛屼竴涓猂ecyclerView閫傞厤鍣ㄥlayouts浠绘剰鎿嶄綔骞朵笉鐭ラ亾鍏蜂綋鐨凚inding绫汇�傚畠浠嶇劧蹇呴渶鍦╫nBindViewHolder鏈熼棿璧嬪�肩粰Binding銆�
鍦ㄨ繖涓緥瀛愪腑锛岃RecyclerView缁戝畾鐨勬墍鏈塴ayouts鏈変竴涓�渋tem鈥濈殑Variable銆傝BindingHolder鏈変竴涓猤etBinding鏂规硶杩斿洖ViewDataBinding銆�
public void onBindViewHolder(BindingHolder holder, int position) {
final T item = mItems.get(position);
holder.getBinding().setVariable(BR.item, item);
holder.getBinding().executePendingBindings();
}
鐩存帴Binding
褰撲竴涓獀ariable鎴杘bservable鍙樺寲鏃讹紝binding浼氬湪璁″垝鍦ㄤ笅涓�甯т箣鍓嶆墽琛屾敼鍙樸�傚彲鑳戒細鍙戠敓寰堝娆★紝浣嗘槸鍦˙inding鏃跺繀椤荤珛鍗虫墽琛屻�傝寮哄埗鎵ц锛屼娇鐢╡xecutePendingBindings()鏂规硶銆�
鍚庡彴绾跨▼
鍙瀹冧笉鏄竴涓泦鍚堬紝浣犲彲浠ュ湪鍚庡彴绾跨▼涓敼鍙樹綘鐨勬暟鎹ā鍨嬨�傚湪鍒ゆ柇鏄惁瑕侀伩鍏嶄换浣曞苟鍙戦棶棰樻椂锛孌ata Binding浼氬姣忎釜Varialbe/field鏈湴鍖栥��
灞炴�etters
姣忓綋缁戝畾鍊肩殑鍙樺寲锛岀敓鎴愮殑Binding绫诲繀椤昏皟鐢╯etter鏂规硶銆侱ata Binding 妗嗘灦鏈夊彲浠ヨ嚜瀹氫箟璧嬪�肩殑鏂规硶銆�
鑷姩Setters
瀵逛簬涓�涓睘鎬э紝Data Binding璇曞浘鎵惧埌setAttribute鏂规硶銆備笌璇ュ睘鎬х殑namespace骞朵笉浠�涔堝叧绯伙紝浠呬粎涓庡睘鎬ф湰韬悕绉版湁鍏炽��
渚嬪锛屾湁鍏砊extView鐨刟ndroid:text灞炴�х殑琛ㄨ揪寮忎細瀵绘壘涓�涓猻etText(String)鐨勬柟娉曘�傚鏋滆〃杈惧紡杩斿洖涓�涓猧nt锛孌ata Binding浼氭悳绱㈢殑setText(int)鏂规硶銆傛敞鎰忥細瑕佽〃杈惧紡杩斿洖姝g‘鐨勭被鍨嬶紝濡傛灉闇�瑕佺殑璇濅娇鐢╟asting銆侱ata Binding浠嶄細宸ヤ綔鍗充娇娌℃湁缁欏畾鍚嶇О鐨勫睘鎬у瓨鍦ㄣ�傜劧鍚庯紝鎮ㄥ彲浠ラ�氳繃Data Binding杞绘澗鍦颁负浠讳綍setter鈥滃垱閫犫�濆睘鎬с�備緥濡傦紝DrawerLayout娌℃湁浠讳綍灞炴�э紝浣嗘湁澶ч噺鐨剆etters銆傛偍鍙互浣跨敤鑷姩setters鏉ヤ娇鐢ㄥ叾涓殑涓�涓��
閲嶅懡鍚嶇殑Setters
涓�浜涙湁setters鐨勫睘鎬ф寜鍚嶇О骞朵笉鍖归厤銆傚浜庤繖浜涙柟娉曪紝灞炴�у彲浠ラ�氳繃BindingMethods娉ㄨВ鐩稿叧鑱斻�傝繖蹇呴』涓庝竴涓寘鍚獴indingMethod娉ㄨВ鐨勭被鐩稿叧鑱旓紝姣忎竴涓敤浜庝竴涓噸鍛藉悕鐨勬柟娉曘�備緥濡傦紝android:tint灞炴�т笌setImageTintList鐩稿叧鑱旓紝鑰屼笉涓巗etTint鐩稿叧銆�
@BindingMethods({
@BindingMethod(type = "android.widget.ImageView",
attribute = "android:tint",
method = "setImageTintList"),
})
浠ヤ笂渚嬪瓙锛屽紑鍙戣�呴渶瑕侀噸鍛藉悕setters鏄笉澶彲鑳戒簡锛宎ndroid鏋舵瀯灞炴�у凡缁忓疄鐜颁簡銆�
鑷畾涔塖etters
鏈変簺灞炴�ч渶瑕佽嚜瀹氫箟缁戝畾閫昏緫銆備緥濡傦紝瀵逛簬android:paddingLeft灞炴�у苟娌℃湁鐩稿叧setter銆傜浉鍙嶏紝setPadding(left, top, right, bottom)鏄瓨鍦ㄥ湪銆備竴涓甫鏈塀indingAdapter娉ㄨВ鐨勯潤鎬佺粦瀹氶�傞厤鍣ㄦ柟娉曞厑璁稿紑鍙戣�呰嚜瀹氫箟setter濡備綍瀵逛簬涓�涓睘鎬х殑璋冪敤銆�
Android鐨勫睘鎬у凡缁忓垱閫犱簡BindingAdapters銆備妇渚嬫潵璇达紝瀵逛簬paddingLeft锛�
@BindingAdapter("android:paddingLeft")
public static void setPaddingLeft(View view, int padding) {
view.setPadding(padding,
view.getPaddingTop(),
view.getPaddingRight(),
view.getPaddingBottom());
}
Binding閫傞厤鍣ㄥ鍏朵粬瀹氬埗绫诲瀷闈炲父鏈夌敤銆備緥濡傦紝鑷畾涔塴oader鍙互鐢ㄦ潵寮傛杞藉叆鍥惧儚銆�
褰撴湁鍐茬獊鏃讹紝寮�鍙戜汉鍛樺垱寤虹殑Binding閫傞厤鍣ㄥ皢瑕嗙洊Data Binding榛樿閫傞厤鍣ㄣ��
鎮ㄤ篃鍙互鍒涘缓鍙互鎺ユ敹澶氫釜鍙傛暟鐨勯�傞厤鍣ㄣ��
@BindingAdapter({"bind:imageUrl", "bind:error"})
public static void loadImage(ImageView view, String url, Drawable error) {
Picasso.with(view.getContext()).load(url).error(error).into(view);
}
濡傛灉瀵逛簬涓�涓狪mageViewimageUrl鍜宔rror閮借浣跨敤骞朵笖imageUrl鏄竴涓猻tring绫诲瀷浠ュ強error鏄竴涓猟rawable鏃讹紝璇ラ�傞厤鍣ㄤ細琚皟鐢ㄣ��
- 鍖归厤鐨勮繃绋嬩腑鑷畾涔塶amespaces灏嗚蹇界暐銆�
- 浣犱篃鍙互涓篈ndroid namespaces鍐欓�傞厤鍣ㄣ��
Binding閫傞厤鍣ㄦ柟娉曞彲鑳戒粠handlers涓幏鍙栨棫鐨勫睘鎬у��. 鍚屾椂鑾峰彇鏂版棫灞炴�у�肩殑鏂规硶搴旇鎶婃棫鐨勫睘鎬у�间綔涓哄弬鏁版斁鍦ㄥ墠杈癸紝绱ц窡鐫�鏄柊鐨勫睘鎬у�硷細
@BindingAdapter("android:paddingLeft")
public static void setPaddingLeft(View view, int oldPadding, int newPadding) {
if (oldPadding != newPadding) {
view.setPadding(newPadding,
view.getPaddingTop(),
view.getPaddingRight(),
view.getPaddingBottom());
}
}
浜嬩欢澶勭悊鍣ㄥ彲浠呭湪鎺ュ彛鎴栬�呮娊璞$被涓娇鐢紝濡傦細
@BindingAdapter("android:onLayoutChange")
public static void setOnLayoutChangeListener(View view, View.OnLayoutChangeListener oldValue,
View.OnLayoutChangeListener newValue) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (oldValue != null) {
view.removeOnLayoutChangeListener(oldValue);
}
if (newValue != null) {
view.addOnLayoutChangeListener(newValue);
}
}
}
褰撶洃鍚櫒鏈夊涓柟娉曟椂锛屽繀椤昏鎷嗗垎鎴愬涓洃鍚櫒銆傚 View.OnAttachStateChangeListener 鏈変袱涓柟娉曪細onViewAttachedToWindow() 鍜� onViewDetachedFromWindow(). 鍒欐垜浠繀椤诲垱寤轰袱涓帴鍙d互涓轰箣鍖哄垎灞炴�у拰澶勭悊鍣細
@TargetApi(VERSION_CODES.HONEYCOMB_MR1)
public interface OnViewDetachedFromWindow {
void onViewDetachedFromWindow(View v);
}
@TargetApi(VERSION_CODES.HONEYCOMB_MR1)
public interface OnViewAttachedToWindow {
void onViewAttachedToWindow(View v);
}
鍥犱负鏀瑰彉鏌愪釜鐩戝惉鍣ㄤ細褰卞搷鍒板叾浠栫殑鐩戝惉鍣紝鎴戜滑蹇呴』缂栫爜涓変釜涓嶅悓鐨刡inding閫傞厤鍣紝涓烘瘡涓睘鎬у悇缂栧啓涓�涓紝骞跺悓鏃朵负涓よ�呬竴璧风紪鍐欎竴涓紝浠栦滑蹇呴』鍚屾椂琚缃��
@BindingAdapter("android:onViewAttachedToWindow")
public static void setListener(View view, OnViewAttachedToWindow attached) {
setListener(view, null, attached);
}
@BindingAdapter("android:onViewDetachedFromWindow")
public static void setListener(View view, OnViewDetachedFromWindow detached) {
setListener(view, detached, null);
}
@BindingAdapter({"android:onViewDetachedFromWindow", "android:onViewAttachedToWindow"})
public static void setListener(View view, final OnViewDetachedFromWindow detach,
final OnViewAttachedToWindow attach) {
if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1) {
final OnAttachStateChangeListener newListener;
if (detach == null && attach == null) {
newListener = null;
} else {
newListener = new OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {
if (attach != null) {
attach.onViewAttachedToWindow(v);
}
}
@Override
public void onViewDetachedFromWindow(View v) {
if (detach != null) {
detach.onViewDetachedFromWindow(v);
}
}
};
}
final OnAttachStateChangeListener oldListener = ListenerUtil.trackListener(view,
newListener, R.id.onAttachStateChangeListener);
if (oldListener != null) {
view.removeOnAttachStateChangeListener(oldListener);
}
if (newListener != null) {
view.addOnAttachStateChangeListener(newListener);
}
}
}
涓婇潰鐨勪緥瀛愯緝閫氬父鑰岃█绋嶅井澶嶆潅涓�浜涳紝鍥犱负 View 閫氳繃 add/remove 鏉ヤ娇鐢ㄧ洃鍚櫒锛岃�屼笉鏄负 View.OnAttachStateChangeListener 浣跨敤涓�涓� set 鏂规硶銆俛ndroid.databinding.adapters.ListenerUtil 绫讳繚鎸佸涔嬪墠鎵�鏈夌洃鍚櫒鐨勮拷韪紝鎵�浠ワ紝浠栦滑蹇呴』浠庣粦瀹氶�傞厤鍣ㄤ腑绉婚櫎銆�
閫氳繃瀵规帴鍙� OnViewDetachedFromWindow 鍜� OnViewAttachedToWindow 鐢� @TargetApi(VERSION_CODES.HONEYCOMB_MR1) 杩涜娉ㄨВ, 鏁版嵁缁戝畾浠g爜鐢熸垚鍣ㄦ槑鐧界洃鍚櫒浠呴渶鍦ㄨ繍琛� Honeycomb 鍙婁互涓婄増鏈殑璁惧涓婄敓鎴愶紝
addOnAttachStateChangeListener(View.OnAttachStateChangeListener) 鏀寔鍚屾牱鐨勭増鏈�.
杞崲
瀵硅薄杞崲
褰撲粠Binding琛ㄨ揪寮忚繑鍥炰竴涓璞★紝涓�涓猻etter浼氫粠鑷姩銆侀噸鍛藉悕浠ュ強鑷畾涔夌殑setters涓�夋嫨銆傝瀵硅薄灏嗚杞崲涓烘墍閫夋嫨鐨剆etter鐨勫弬鏁扮被鍨嬨��
杩欐槸涓轰簡鏂逛究閭d簺浣跨敤ObservableMaps鏉ヤ繚瀛樻暟鎹�備緥濡傦細
鍦╱serMap杩斿洖涓�涓璞″苟涓旇瀵硅薄灏嗚嚜鍔ㄨ浆鎹负setText(CharSequence)鐨勫弬鏁扮被鍨嬨�傚綋鏈夊叧鍙傛暟绫诲瀷鍙兘娣蜂贡鏃讹紝寮�鍙戜汉鍛橀渶瑕佸湪琛ㄨ揪寮忎腑杞崲銆�
鑷畾涔夎浆鎹�
鏈夋椂鍊欒浆鎹㈠簲璇ユ槸鑷姩鐨勫湪鐗瑰畾绫诲瀷涔嬮棿銆備緥濡傦紝璁剧疆鑳屾櫙鐨勬椂鍊欙細
杩欓噷锛岃儗鏅渶瑕丏rawable瀵硅薄锛屼絾棰滆壊鏄竴涓暣鏁般�備笉绠′綍鏃舵湁Drawable骞朵笖杩斿洖鍊兼槸涓�涓暣鏁帮紝閭d箞鏁存暟绫诲瀷浼氳杞崲涓篊olorDrawable銆傝繖涓浆鎹㈡槸閫氳繃浣跨敤甯︽湁BindingConversion娉ㄨВ鐨勯潤鎬佹柟娉曞畬鎴愮殑锛�
@BindingConversion
public static ColorDrawable convertColorToDrawable(int color) {
return new ColorDrawable(color);
}
娉ㄦ剰锛氳浆鎹粎浠呭彂鐢熷湪setter绾у埆锛屽洜姝ゅ畠涓嶅厑璁镐互涓嬫贩鍚堢被鍨嬶細
Android Studio 瀵规暟鎹粦瀹氱殑鏀寔
Android Studio 鏀寔鏁版嵁缁戝畾琛ㄨ揪寮忕殑璇硶楂樹寒锛屽苟鍙湪缂栬緫鍣ㄤ腑鏍囩ず琛ㄨ揪寮忚娉曢敊璇��
棰勮绐楁牸鍙樉绀烘暟鎹粦瀹氳〃杈惧紡鐨勯璁鹃粯璁ゅ�硷紝鍦ㄤ笅闈㈢殑渚嬪瓙涓紝棰勮绐楁牸鍦� TextView 涓樉绀洪粯璁ょ殑 PLACEHOLDER 鏂囨湰鍊笺��
濡傛灉浣犻渶瑕佸湪璁捐鏈熼棿鏄剧ず榛樿鍊硷紝浣犱篃鍙互浣跨敤 tools:attributes 浠f浛榛樿琛ㄨ揪寮忓�笺��
鍙傝�冭祫鏂�
- 鐣岄潰涔嬩笅锛氳繕鍘熺湡瀹炵殑MV*妯″紡
- Android 瀹樻柟鑻辨枃鎸囧崡
- Data Binding锛堟暟鎹粦瀹氾級鐢ㄦ埛鎸囧崡