Android MVVM + DataBinding的開(kāi)發(fā)框架

1.mvc,mvp,mvvm

  • 前面講了mvc和mvp的框架及其優(yōu)缺點(diǎn),如果說(shuō)mvp是mvc的升級(jí)版,那mvvm算是mvp的升級(jí)版.但是databinding,絕對(duì)是一個(gè)程序員夢(mèng)寐以求的框架,再也不用寫(xiě)findViewById了,聽(tīng)著是不是很興奮.

2. 神作databinding

  1. 廢話(huà)不講了,直接開(kāi)始,感受神作的魅力
  2. 先看看以前的寫(xiě)法
  3. 再看看databinding的寫(xiě)法

3 總結(jié)

  • 優(yōu)勢(shì)
  1. Activity/Fragment代碼量減少(提高開(kāi)發(fā)效率,尤其是findViewById)
  2. databinding可以判斷null值,不會(huì)報(bào)空指針
  3. 在布局中可以進(jìn)行簡(jiǎn)單的表達(dá)式,減少java邏輯代碼
  4. 0反射,全部在編譯時(shí)期運(yùn)行,不影響程序運(yùn)行,性能超過(guò)手寫(xiě)代碼
  5. 自動(dòng)檢測(cè)空指針,避免crash
  • 劣勢(shì)
  1. ide支持不完善,有時(shí)候需要build一下,建議一起把ide升級(jí)到最新,我也不是最新的
  2. 錯(cuò)誤查找不方便

4 DataBinding簡(jiǎn)單教程

  • 在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>
         //數(shù)據(jù)部分
    </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}表示數(shù)據(jù)ui雙向綁定,ui變->數(shù)據(jù)變,數(shù)據(jù)變->ui變
        //@{loginoutvm.username} ui數(shù)據(jù)單向綁定, 數(shù)據(jù)變->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處理步驟
    開(kāi)始編譯->處理layout文件->解析表達(dá)式->java編譯->解析依賴(lài)->找到setter函數(shù)
    并且databinding會(huì)緩存表達(dá)式,減少表達(dá)式的計(jì)算
    只操作一遍findViewById
    利用標(biāo)記來(lái)更新數(shù)據(jù),減少數(shù)據(jù)更新頻率
  • databinding支持的表達(dá)式
  1. 數(shù)學(xué)表達(dá)式: + - / * %
  2. 字符串拼接 +
  3. 邏輯表達(dá)式 && ||
  4. 位操作符 & | ^
  5. 一元操作符 + - ! ~
  6. 位移操作符 >> >>> <<
  7. 比較操作符 == > < >= <=
  8. instanceof
  9. 分組操作符 ()
  10. 字面量 - character, String, numeric, null
  11. 強(qiáng)轉(zhuǎn)、方法調(diào)用
  12. 字段訪(fǎng)問(wèn)
  13. 數(shù)組訪(fǎng)問(wèn) []
  14. 三元操作符 ?:
  15. 聚合判斷(Null Coalescing Operator)語(yǔ)法 ‘??’
    雖然支持這么多運(yùn)算符,但還是建議,稍微復(fù)雜點(diǎn)的不要放在xml里進(jìn)行
  • 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支持動(dòng)畫(huà)過(guò)度
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.*
當(dāng)使用List時(shí)候,ide會(huì)顯示紅色,但是能編譯,這個(gè)是google官網(wǎng)給出的解釋

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容