Android MVVM 模式

1.MVVM模式分為Model,View,ViewModel 三個部分

(1).Model:數(shù)據(jù)層,包含數(shù)據(jù)實(shí)體和對數(shù)據(jù)實(shí)體的操作
(2).View:界面層,對應(yīng)于Activity,XML,View,負(fù)責(zé)數(shù)據(jù)顯示以及用戶交互。
(3).ViewModel:關(guān)聯(lián)層,將Model和View進(jìn)行綁定,Model或者View更改時,實(shí)時刷新對方。

注意點(diǎn)

1.View只做和UI相關(guān)的工作,不涉及任何業(yè)務(wù)邏輯,不涉及操作數(shù)據(jù),不處理數(shù)據(jù)。UI和數(shù)據(jù)嚴(yán)格的分開
2.ViewModel只做和業(yè)務(wù)邏輯相關(guān)的工作,不涉及任何和UI相關(guān)的操作,不持有控件引用,不更新UI。

2.MVVM模式圖

image.png

3.Android MVVM模式圖

image.png

View
顯而易見Activity/Fragment便是MVVM中的View,當(dāng)收到ViewModel傳遞過來的數(shù)據(jù)時,Activity/Fragment負(fù)責(zé)將數(shù)據(jù)以你喜歡的方式顯示出來。View還包括ViewDataBinding,上面中并沒有體現(xiàn)。

ViewModel
ViewModel作為Activity/Fragment與其他組件的連接器。負(fù)責(zé)轉(zhuǎn)換和聚合Model中返回的數(shù)據(jù),使這些數(shù)據(jù)易于展示,并把這些數(shù)據(jù)改變即時通知給Actvity/Fragment。
ViewModel是具有生命周期意識的,當(dāng)Activity/Fragment銷毀時ViewModel的onClear方法會被回調(diào),你可以在這里做一些清理工作。LiveData是具有生命周期意識的一個可觀察的數(shù)據(jù)持有者,ViewModel中的數(shù)據(jù)有LiveData持有,并且只有當(dāng)Activity/Fragment處于活動時才會通知UI數(shù)據(jù)的改變,避免無用的刷新UI。

Model
Repository及其下方就是model了。Repository負(fù)責(zé)提取和處理數(shù)據(jù)。數(shù)據(jù)來源可以是本地?cái)?shù)據(jù)庫,也可以來自網(wǎng)絡(luò),這些數(shù)據(jù)統(tǒng)一有Repository處理,對應(yīng)隱藏?cái)?shù)據(jù)來源以及獲取方式。

Binder綁定器
Android中的數(shù)據(jù)綁定技術(shù)由DataBinding和LiveData共同實(shí)現(xiàn)。當(dāng)Activity/Fragment接收到來自ViewModel中的新數(shù)據(jù)時(由LiveData自動通知數(shù)據(jù)的改變),將這些數(shù)據(jù)通過DataBinding綁定到ViewDataBinding中,UI將會自動刷新。

4.MVVM的優(yōu)勢和劣勢

1),使得M,V,VM的解耦更加徹底,在mvp模式中,p需要持有V的引用,才能去刷新UI,在MVVM模式中,View和Model使用databingding進(jìn)行雙向綁定,一方改變會直接通知另外一方,使得viewModel能專注于業(yè)務(wù)邏輯的處理,而不需要去關(guān)心UI刷新。
2),不會像MVC一樣導(dǎo)致Activity中代碼量巨大,也不會像MVP一樣出現(xiàn)大量的View接口(Presente與View是通過接口進(jìn)行交互的)。項(xiàng)目結(jié)構(gòu)更加低耦合。

MVVM的劣勢

1),數(shù)據(jù)綁定使得Bug很難被調(diào)試。
2),一個大的模塊中,model也會很大,雖然使用方便了也很容易保證了數(shù)據(jù)的一致性,但是長期持有,不釋放內(nèi)存,就造成了花費(fèi)更多的內(nèi)存。
3),數(shù)據(jù)雙向綁定不利于代碼重用。客戶端開發(fā)最常用的重用時View,但是數(shù)據(jù)雙向綁定技術(shù),讓你在一個View都綁定了一個model,不同模塊的model都不同,那就不能簡單重用View了。

5.Databinding框架

Databinding和MVVM的關(guān)系

MVVM是一種架構(gòu)模式,DataBinding是一個實(shí)現(xiàn)數(shù)據(jù)和UI綁定的框架,是實(shí)現(xiàn)MVVM模式的工具。

引入DataBinding
引入DataBinding的方式很簡單,我們只需要在App的build.gradle添加如下代碼即可

android{
.....
dataBinding {
        enabled = true
    }
}

Databinding常用方法

1).BindingAdapter注解設(shè)置自定義屬性

public class ImageHelper {

    /**
     * 1.加載圖片,無需手動調(diào)用此方法
     * 2.使用@BindingAdapter注解設(shè)置自定義屬性的名稱,imageUrl就是屬性的名稱,
     * 當(dāng)ImageView中使用imageUrl屬性時,會自動調(diào)用loadImage方法,
     *
     * @param imageView ImageView
     * @param url       圖片地址
     */
@BindingAdapter({"imageUrl", "placeHolder",“error"})
public static void loadImage(ImageView view, String url, Drawable holderDrawable, Drawable errorDrawable) {
     Glide.with(imageView.getContext())  
                .load(url)  
                .placeholder(holderDrawable)  
                .error(errorDrawable)  
                .into(imageView);
}
}

xml中使用自定義屬性

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <import type="com.databindingdemo.bean.UserBean" />
        <variable
            name="user"
            type="UserBean" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:orientation="vertical">

        <!-- 當(dāng)imageUrl屬性存在時,會自動調(diào)用ImageHelper的loadImage方法 -->
        <ImageView
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:scaleType="centerCrop"
            app:error="@{user.errorUrl}"
            app:placeHolder="@{user.placeHolder}"
            app:imageUrl="@{user.picUrl}" />
    </LinearLayout>
</layout>   

Activity中設(shè)置圖片 的url

public class MainActivity extends AppCompatActivity  {
    //用戶頭像
    private static final String URL_USER_PIC = "http://xxx/xx.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        UserBean userBean = new UserBean(URL_USER_PIC, "張三", 24);
        binding.setUser(userBean);
    }
}

DataBinding動態(tài)更新數(shù)據(jù)的兩種方式

1).BaseObservable
這個類也實(shí)現(xiàn)了字段變動的通知,在變量的getter上使用Bindable注解,并通過notifyPropertyChanged通知更新即可。

public class DoubleBindBean extends BaseObservable {

 // 用 @Bindable 標(biāo)記過 getxxx() 方法會在 BR 中生成一個 entry。 當(dāng)數(shù)據(jù)發(fā)生變化時需要調(diào)用 notifyPropertyChanged(BR.content) 通知系統(tǒng) BR.content這個 entry 的數(shù)據(jù)已經(jīng)發(fā)生變化以更新UI。

    private String content; //內(nèi)容

    public DoubleBindBean(String content) {
        this.content = content;
    }

    @Bindable
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
        notifyPropertyChanged(BR.content); //通知系統(tǒng)數(shù)據(jù)源發(fā)生變化,刷新UI界面
    }
}

2).ObservableFiled
如果想要省時或者數(shù)據(jù)類的字段很少的話,可以使用ObservableFiled以及它的派生ObservableBoolean,ObservableByte,ObservableChar,ObservableShort,ObservableInt,ObservableLong,ObservableFloat,ObservableDouble,ObservableParcelable等。

public class DoubleBindBean2 {
    //變量需要為public
    public final ObservableField<String> username = new ObservableField<>();
}

Observable Collections
除了支持ObservableField,ObservableBoolean,ObservableInt等基礎(chǔ)變量類型以外,當(dāng)然也支持集合框架拉,比如:ObservableArrayMap,ObservableArrayList。使用和普通的Map、List基本相同.

3).雙向綁定
以上兩個說的都是單向綁定,數(shù)據(jù)的流向是單向的,下面我們說說雙向綁定。
幸運(yùn)的是,Android原生控件中,絕大多數(shù)的雙向綁定使用場景,DataBinding都已經(jīng)幫我們實(shí)現(xiàn)好了:可以參考包名androidx.databinding.adapters下實(shí)現(xiàn)了系統(tǒng)基本所有原生控件雙向綁定的Adapter類

image.png

這意味著我們并不需要手動去實(shí)現(xiàn)復(fù)雜的雙向綁定,以EditText為例,我們只需要通過@=(表達(dá)式)進(jìn)行雙向綁定:

<EditText
    android:id="@+id/etPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@={ viewModel.password }" />

相比單向綁定,只需要多一個=符號,就能保住View層和ViewModel層的狀態(tài)同步了。

難點(diǎn)在哪?

雙向綁定定義好之后,使用起來很簡單,但是定義卻稍微比單向綁定麻煩一些,即使原生控件的DataBinding已經(jīng)幫助我們實(shí)現(xiàn)好了,對于三方的控件或者自定義控件,還需要我們自己實(shí)現(xiàn)。
我們已SwipeRefreshLayout為列,讓我們來看看其雙向綁定實(shí)現(xiàn)的方式:

package com.geespace.doublebinding

import android.util.Log
import androidx.databinding.BindingAdapter
import androidx.databinding.InverseBindingAdapter
import androidx.databinding.InverseBindingListener
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout

/**
 * Created by mao  on 2020/3/4.
Description:
 */

object SwipeRefreshLayoutBinding {

  //先實(shí)現(xiàn)單向綁定
    @JvmStatic
    @BindingAdapter("swipeRefreshLayout_refreshing")
    fun setSwipeRefreshLayoutRefreshing(
        swipeRefreshLayout: SwipeRefreshLayout,
        newValue: Boolean) {
        Log.e("swipeBinding", "setSwipeRefreshLayoutRefreshing:$newValue")
        if (swipeRefreshLayout.isRefreshing != newValue) //不要忘了防止死循環(huán)!
      //保證,只有View狀態(tài)發(fā)生了變更,才會去更新UI
            swipeRefreshLayout.isRefreshing = newValue
    }

    @JvmStatic
    @InverseBindingAdapter(
        attribute = "swipeRefreshLayout_refreshing",
        event = "swipeRefreshLayout_refreshingAttrChanged" //2 .匹配 1
    )
    fun isSwipeRefreshLayoutRefreshing(swipeRefreshLayout: SwipeRefreshLayout): Boolean =
        swipeRefreshLayout.isRefreshing

    @JvmStatic
    @BindingAdapter(
        "swipeRefreshLayout_refreshingAttrChanged",  //1.注意默認(rèn)是AttrChanged結(jié)尾
        requireAll = false)
    fun setOnRefreshListener(
        swipeRefreshLayout: SwipeRefreshLayout,
        bindingListener: InverseBindingListener?) {
        Log.e("swipeBinding","setOnRefreshingListener")

        if (bindingListener != null)
            swipeRefreshLayout.setOnRefreshListener {
                bindingListener.onChange()   //每當(dāng)swipeRefreshLayout刷新狀態(tài)被用戶的      
                              //操作改變,我們都能夠在這里監(jiān)聽到,
                              //并交給InverseBindingListener這個 信使 去通知DataBinding:

   
            }
    }
}

xml文件如下:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
<variable
    name="viewModel"
    type="com.geespace.doublebinding.BaseViewModel" />
    </data>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/swipe"
        app:swipeRefreshLayout_refreshing="@={viewModel.refreshing }">

        <TextView
            android:layout_width="100dp"
            android:text="消失掉吧"
            android:id="@+id/txt_hide"
            android:padding="20dp"
            android:layout_height="100dp" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</layout>

refreshing參數(shù)如下:

class BaseViewModel :ViewModel(){
        var refreshing:MutableLiveData<Boolean> = MutableLiveData()
}

Activity的代碼如下:

class MainActivity2 :AppCompatActivity(){
    lateinit var swipeBinding:SwipeBinding
    lateinit var viewModel:BaseViewModel
    var handler:Handler=Handler()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        swipeBinding=DataBindingUtil.setContentView(this,R.layout.swipe)

        viewModel=ViewModelProvider(this).get(BaseViewModel::class.java)


        swipeBinding.viewModel=viewModel
        swipeBinding.lifecycleOwner=this //一定要設(shè)置lifeCycleOwner 否則LiveData雙向綁定不起作用


      viewModel.refreshing.observe(this, Observer {
            Log.e("mainActivity2,","isRefreshing:${it}")

        })


        txt_hide.setOnClickListener {
           viewModel.refreshing.postValue(false)
        }

    }
}

上面有個地方需要注意下:

當(dāng)使用LiveData進(jìn)行雙向綁定的時候 一定要記得調(diào)用binding.setLifeCycleOwner方法,否則LiveData數(shù)據(jù)改變的時候,View沒法收到通知,切記!!可以想象到binding內(nèi)部使用這個LifecycleOwner給liveData設(shè)置了監(jiān)聽。

下面就是這個方法的詳細(xì)說明:

/**
     * Sets the {@link LifecycleOwner} that should be used for observing changes of
     * LiveData in this binding. If a {@link LiveData} is in one of the binding expressions
     * and no LifecycleOwner is set, the LiveData will not be observed and updates to it
     * will not be propagated to the UI.
     *
     * @param lifecycleOwner The LifecycleOwner that should be used for observing changes of
     *                       LiveData in this binding.
     */
    @MainThread
    public void setLifecycleOwner(@Nullable LifecycleOwner lifecycleOwner) {
        if (mLifecycleOwner == lifecycleOwner) {
            return;
        }
        if (mLifecycleOwner != null) {
            mLifecycleOwner.getLifecycle().removeObserver(mOnStartListener);
        }
        mLifecycleOwner = lifecycleOwner;
        if (lifecycleOwner != null) {
            if (mOnStartListener == null) {
                mOnStartListener = new OnStartListener(this);
            }
            lifecycleOwner.getLifecycle().addObserver(mOnStartListener);
        }
        for (WeakListener<?> weakListener : mLocalFieldObservers) {
            if (weakListener != null) {
                weakListener.setLifecycleOwner(lifecycleOwner);
            }
        }
    }

雙向綁定使用到的注解

1)@InverseBindingAdapter

1.作用于方法,方法須為公共靜態(tài)方法。
2.方法的第一個參數(shù)必須為View類型,如TextView等
3.需要與@BindingAdapter配合使用

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
public @interface InverseBindingAdapter {

    String attribute();

    String event() default "";
}

attribute:String類型,必填,表示當(dāng)值發(fā)生變化時,要從哪個屬性中檢索這個變化的值,示例:"android:text"
event: String類型,非必填;如果填寫,則使用填寫的內(nèi)容作為event的值;如果不填,在編譯時會根據(jù)attribute的屬性名再加上后綴“AttrChanged”生成一個新的屬性作為event的值,舉個例子:attribute屬性的值為”android:text”,那么默認(rèn)會在”android:text”后面追加”AttrChanged”字符串,生成”android:textAttrChanged”字符串作為event的值.
event屬性的作用: 當(dāng)View的值發(fā)生改變時用來通知dataBinding值已經(jīng)發(fā)生改變了。開發(fā)者一般需要使用@BindingAdapter創(chuàng)建對應(yīng)屬性來響應(yīng)這種改變

2)@InverseBindingMethod與@InverseBindingMethods

1.@InverseBindingMethods注解用于標(biāo)記類
2.@InverseBindingMethod注解需要與@InverseBindingMethods注解結(jié)合使用才能發(fā)揮其功效
3.@InverseBindingMethods需要與@BindingAdapter配合使用才能發(fā)揮功效
用法與示列:

@InverseBindingMethods({
        @InverseBindingMethod(type = SeekBar.class, attribute = "android:progress"),
})
public class SeekBarBindingAdapter {}

@InverseBindingMethod

@Target(ElementType.ANNOTATION_TYPE)
public @interface InverseBindingMethod {

    Class type();

    String attribute();

    String event() default "";

    String method() default "";
}

type:Class類型,必填,如:SeekBar.class
attribute:String類型,必填,如:android:progress
event:String類型,非必填,屬性值的生成規(guī)則以及作用和@InverseBindingAdapter中的event一樣。
method:String類型,非必填,對于什么時候填什么時候不填,這里舉個例子說明:比如SeekBar,它有android:progress屬性,也有g(shù)etProgress方法【你沒看錯,就是getProgress,不是setProgress】,所以對于SeekBar的android:progress屬性,不需要明確指定method,因?yàn)椴恢付╩ethod時,默認(rèn)的生成規(guī)則就是前綴“get”加上屬性名,組合起來就是getProgress,而剛才也說了,getProgress方法在seekBar中是存在的,所以不用指定method也可以,但是如果默認(rèn)生成的方法getXxx在SeekBar中不存在,而是其他方法比如getRealXxx,那么我們就需要通過method屬性,指明android:xxx對應(yīng)的get方法是getRealXxx,這樣dataBinding在生成代碼時,就使用getRealXxx生成代碼了;從宏觀上來看,@InverseBindingMethod的method屬性的生成規(guī)則與@BindingMethod的method屬性的生成規(guī)則其實(shí)是類似的

3)@InverseMethod

作用于方法,
用于雙向綁定
@InverseMethod 注解是一個相對獨(dú)立的注解,不需要其他注解的配合就可以發(fā)揮它強(qiáng)大的作用,它的作用就是為某個方法指定一個相反的方法。它有一個String類型的必填屬性:value,用來存放與當(dāng)前方法對應(yīng)的相反方法
正方法與反方法的要求:

  • 正方法與反方法的參數(shù)數(shù)量必須相同
  • 正方法的最終參數(shù)的類型與反方法的返回值必須相同
  • 正方法的返回值類型必須與反方法的最終參數(shù)類型相同。
    用列如下:
@InverseMethod("convertIntToString")
public static int convertStringToInt(String value) {
    try {
        return Integer.parseInt(value);
    } catch (NumberFormatException e) {
        return -1;
    }
}
public static String convertIntToString(int value) {
    return String.valueOf(value);
}

4)@Bindable

該注解用于雙向綁定,需要與 notifyPropertyChanged()方法結(jié)合使用
該注解用于標(biāo)記實(shí)體類中的get方法或“is”開頭的方法,且實(shí)體類必須繼承BaseObserable.
示例用法

public class User extends BaseObservable {

    private String name;
    private int age;
    private String sex;
    private boolean isStudent;

    @Bindable
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(com.qiangxi.databindingdemo.BR.name);
    }

    @Bindable
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
        notifyPropertyChanged(com.qiangxi.databindingdemo.BR.age);
    }

    @Bindable
    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
        notifyPropertyChanged(com.qiangxi.databindingdemo.BR.sex);
    }

    @Bindable
    public boolean isStudent() {
        return isStudent;
    }

    public void setStudent(boolean student) {
        isStudent = student;
        notifyPropertyChanged(com.qiangxi.databindingdemo.BR.student);
    }

    @Bindable({"name", "age", "sex", "isStudent"})
    public String getAll() {
        return "姓名:" + name + ",年齡=" + age + ",性別:" + sex + ",是不是學(xué)生=" + isStudent;
    }
}

@Bindable注解是用來干什么的?
使用@Bindable注解標(biāo)記的get方法,在編譯時,會在BR類中生成對應(yīng)的字段,然后與notifyPropertyChanged()方法配合使用,當(dāng)該字段中的數(shù)據(jù)被修改時,dataBinding會自動刷新對應(yīng)view的數(shù)據(jù),而不用我們在拿到新數(shù)據(jù)后重新把數(shù)據(jù)在setText()一遍,就憑這一點(diǎn),dataBinding就可以簡化大量的代碼

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

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