1.mvc,mvp,mvvm
- 前面講了mvc和mvp的框架及其優缺點,如果說mvp是mvc的升級版,那mvvm算是mvp的升級版.但是databinding,絕對是一個程序員夢寐以求的框架,再也不用寫
findViewById
了,聽著是不是很興奮.
2. 神作databinding
- 廢話不講了,直接開始,感受神作的魅力
- 先看看以前的寫法
- 再看看databinding的寫法
3 總結
- 優勢
- Activity/Fragment代碼量減少(提高開發效率,尤其是findViewById)
- databinding可以判斷null值,不會報空指針
- 在布局中可以進行簡單的表達式,減少java邏輯代碼
- 0反射,全部在編譯時期運行,不影響程序運行,性能超過手寫代碼
- 自動檢測空指針,避免crash
- 劣勢
- ide支持不完善,有時候需要build一下,建議一起把ide升級到最新,我也不是最新的
- 錯誤查找不方便
4 DataBinding簡單教程
- 在app的build.gradle中添加
android {
...
dataBinding {
enabled = true
}
}
- 修改布局xml文件
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.android.architecture.blueprints.todoapp.demo_mvvm.LoginOutActivity">
<data>
//數據部分
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
//布局部分
</LinearLayout>
</layout>
- 在activity中添加代碼,binding xml文件
private ActivityLoginOutBinding activityLoginOutBinding;
//onCreate
activityLoginOutBinding = DataBindingUtil.setContentView(this, R.layout.activity_login_out);
- databinding的使用
<data>
<variable
name="view"
type="com.example.android.architecture.blueprints.todoapp.demo_mvvm.LoginOutActivity"/>
<variable
name="loginoutvm"
type="com.example.android.architecture.blueprints.todoapp.demo_mvvm.LoginOutVM"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
//binding屬性, @={loginoutvm.username}表示數據ui雙向綁定,ui變->數據變,數據變->ui變
//@{loginoutvm.username} ui數據單向綁定, 數據變->ui變
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@={loginoutvm.username}"/>
//binding方法
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:text="登錄"
android:onClick="@{(v)view->login(v)}"
/>
</LinearLayout>
- databinding處理步驟
開始編譯->處理layout文件->解析表達式->java編譯->解析依賴->找到setter函數
并且databinding會緩存表達式,減少表達式的計算
只操作一遍findViewById
利用標記來更新數據,減少數據更新頻率 - databinding支持的表達式
- 數學表達式: + - / * %
- 字符串拼接 +
- 邏輯表達式 && ||
- 位操作符 & | ^
- 一元操作符 + - ! ~
- 位移操作符 >> >>> <<
- 比較操作符 == > < >= <=
- instanceof
- 分組操作符 ()
- 字面量 - character, String, numeric, null
- 強轉、方法調用
- 字段訪問
- 數組訪問 []
- 三元操作符 ?:
- 聚合判斷(Null Coalescing Operator)語法 ‘??’
雖然支持這么多運算符,但還是建議,稍微復雜點的不要放在xml里進行
- databinding支持include
支持
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
bind:user="@{user}"
layout="@layout/name"/>
<include
bind:user="@{user}"
layout="@layout/contact"/>
</LinearLayout>
不支持
<merge>
<include
bind:user="@{user}"
layout="@layout/name"/>
<include
bind:user="@{user}"
layout="@layout/contact"/>
</merge>
- databinding支持動畫過度
activityLoginOutBinding.addOnRebindCallback(new OnRebindCallback() {
@Override
public boolean onPreBind(ViewDataBinding binding) {
ViewGroup viewGroup = (ViewGroup) binding.getRoot();
TransitionManager.beginDelayedTransition(viewGroup);
return true;
}
});
- databinding還支持List和Map
<data>
<import type="com.example.User"/>
<import type="java.util.List"/>
<variable name="user" type="User"/>
<variable name="userList" type="List<User>"/>
</data>
*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.*
當使用List時候,ide會顯示紅色,但是能編譯,這個是google官網給出的解釋