MVVM 鍜� Android Data Binding

鍦� 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 杩涜澶勭悊涓氬姟閫昏緫銆�

MVVM 鍜� Android Data Binding_第1张图片
MVC Pattern

MVC 閫氬父鐨勫鐞嗘椂搴忓涓嬶細

  1. View 鎺ュ彈鐢ㄦ埛鐨勪氦浜掕姹傦紱
  2. View 灏嗚姹傝浆浜ょ粰 Controller锛�
  3. Controller 鎿嶄綔 Model 杩涜鏁版嵁鏇存柊锛�
  4. 鏁版嵁鏇存柊涔嬪悗锛孧odel 閫氱煡 View 鏁版嵁鍙樺寲锛�
  5. View 鏄剧ず鏇存柊涔嬪悗鐨勬暟鎹��

閫氬父 Model 浣跨敤 Observer 妯″紡閫氱煡 View 鏁版嵁鍙樺寲锛�

MVVM 鍜� Android Data Binding_第2张图片
MVC with Observer

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锛�

MVVM 鍜� Android Data Binding_第3张图片
MVP Pattern

MVP 閫氬父鐨勮皟鐢ㄦ椂搴忓涓嬶細

  1. View 鎺ュ彈鐢ㄦ埛鐨勪氦浜掕姹傦紱
  2. View 灏嗚姹傝浆浜ょ粰 Presenter锛�
  3. Presenter 鎿嶄綔 Model 杩涜涓氬姟澶勭悊锛�
  4. Model 閫氱煡 Presenter 鏁版嵁鍙戠敓鍙樺寲锛�
  5. 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锛�

MVVM 鍜� Android Data Binding_第4张图片
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 鍜� Android Data Binding_第5张图片
MVVM Pattern

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 user = new ObservableArrayList<>();
user.add("Google");
user.add("Inc.");
user.add(17);
 
 

鍦╨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锛堟暟鎹粦瀹氾級鐢ㄦ埛鎸囧崡

你可能感兴趣的:(MVVM 鍜� Android Data Binding)