1.MVVM模式分為Model,View,ViewModel 三個部分
(1).Model:數據層,包含數據實體和對數據實體的操作
(2).View:界面層,對應于Activity,XML,View,負責數據顯示以及用戶交互。
(3).ViewModel:關聯層,將Model和View進行綁定,Model或者View更改時,實時刷新對方。
注意點
1.View只做和UI相關的工作,不涉及任何業務邏輯,不涉及操作數據,不處理數據。UI和數據嚴格的分開
2.ViewModel只做和業務邏輯相關的工作,不涉及任何和UI相關的操作,不持有控件引用,不更新UI。
2.MVVM模式圖
3.Android MVVM模式圖
View
顯而易見Activity/Fragment便是MVVM中的View,當收到ViewModel傳遞過來的數據時,Activity/Fragment負責將數據以你喜歡的方式顯示出來。View還包括ViewDataBinding,上面中并沒有體現。
ViewModel
ViewModel作為Activity/Fragment與其他組件的連接器。負責轉換和聚合Model中返回的數據,使這些數據易于展示,并把這些數據改變即時通知給Actvity/Fragment。
ViewModel是具有生命周期意識的,當Activity/Fragment銷毀時ViewModel的onClear方法會被回調,你可以在這里做一些清理工作。LiveData是具有生命周期意識的一個可觀察的數據持有者,ViewModel中的數據有LiveData持有,并且只有當Activity/Fragment處于活動時才會通知UI數據的改變,避免無用的刷新UI。
Model
Repository及其下方就是model了。Repository負責提取和處理數據。數據來源可以是本地數據庫,也可以來自網絡,這些數據統一有Repository處理,對應隱藏數據來源以及獲取方式。
Binder綁定器
Android中的數據綁定技術由DataBinding和LiveData共同實現。當Activity/Fragment接收到來自ViewModel中的新數據時(由LiveData自動通知數據的改變),將這些數據通過DataBinding綁定到ViewDataBinding中,UI將會自動刷新。
4.MVVM的優勢和劣勢
1),使得M,V,VM的解耦更加徹底,在mvp模式中,p需要持有V的引用,才能去刷新UI,在MVVM模式中,View和Model使用databingding進行雙向綁定,一方改變會直接通知另外一方,使得viewModel能專注于業務邏輯的處理,而不需要去關心UI刷新。
2),不會像MVC一樣導致Activity中代碼量巨大,也不會像MVP一樣出現大量的View接口(Presente與View是通過接口進行交互的)。項目結構更加低耦合。
MVVM的劣勢
1),數據綁定使得Bug很難被調試。
2),一個大的模塊中,model也會很大,雖然使用方便了也很容易保證了數據的一致性,但是長期持有,不釋放內存,就造成了花費更多的內存。
3),數據雙向綁定不利于代碼重用。客戶端開發最常用的重用時View,但是數據雙向綁定技術,讓你在一個View都綁定了一個model,不同模塊的model都不同,那就不能簡單重用View了。
5.Databinding框架
Databinding和MVVM的關系
MVVM是一種架構模式,DataBinding是一個實現數據和UI綁定的框架,是實現MVVM模式的工具。
引入DataBinding
引入DataBinding的方式很簡單,我們只需要在App的build.gradle添加如下代碼即可
android{
.....
dataBinding {
enabled = true
}
}
Databinding常用方法
1).BindingAdapter注解設置自定義屬性
public class ImageHelper {
/**
* 1.加載圖片,無需手動調用此方法
* 2.使用@BindingAdapter注解設置自定義屬性的名稱,imageUrl就是屬性的名稱,
* 當ImageView中使用imageUrl屬性時,會自動調用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">
<!-- 當imageUrl屬性存在時,會自動調用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中設置圖片 的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動態更新數據的兩種方式
1).BaseObservable
這個類也實現了字段變動的通知,在變量的getter上使用Bindable注解,并通過notifyPropertyChanged通知更新即可。
public class DoubleBindBean extends BaseObservable {
// 用 @Bindable 標記過 getxxx() 方法會在 BR 中生成一個 entry。 當數據發生變化時需要調用 notifyPropertyChanged(BR.content) 通知系統 BR.content這個 entry 的數據已經發生變化以更新UI。
private String content; //內容
public DoubleBindBean(String content) {
this.content = content;
}
@Bindable
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
notifyPropertyChanged(BR.content); //通知系統數據源發生變化,刷新UI界面
}
}
2).ObservableFiled
如果想要省時或者數據類的字段很少的話,可以使用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等基礎變量類型以外,當然也支持集合框架拉,比如:ObservableArrayMap,ObservableArrayList。使用和普通的Map、List基本相同.
3).雙向綁定
以上兩個說的都是單向綁定,數據的流向是單向的,下面我們說說雙向綁定。
幸運的是,Android原生控件中,絕大多數的雙向綁定使用場景,DataBinding都已經幫我們實現好了:可以參考包名androidx.databinding.adapters下實現了系統基本所有原生控件雙向綁定的Adapter類
這意味著我們并不需要手動去實現復雜的雙向綁定,以EditText為例,我們只需要通過@=(表達式)進行雙向綁定:
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={ viewModel.password }" />
相比單向綁定,只需要多一個=符號,就能保住View層和ViewModel層的狀態同步了。
難點在哪?
雙向綁定定義好之后,使用起來很簡單,但是定義卻稍微比單向綁定麻煩一些,即使原生控件的DataBinding已經幫助我們實現好了,對于三方的控件或者自定義控件,還需要我們自己實現。
我們已SwipeRefreshLayout為列,讓我們來看看其雙向綁定實現的方式:
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 {
//先實現單向綁定
@JvmStatic
@BindingAdapter("swipeRefreshLayout_refreshing")
fun setSwipeRefreshLayoutRefreshing(
swipeRefreshLayout: SwipeRefreshLayout,
newValue: Boolean) {
Log.e("swipeBinding", "setSwipeRefreshLayoutRefreshing:$newValue")
if (swipeRefreshLayout.isRefreshing != newValue) //不要忘了防止死循環!
//保證,只有View狀態發生了變更,才會去更新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.注意默認是AttrChanged結尾
requireAll = false)
fun setOnRefreshListener(
swipeRefreshLayout: SwipeRefreshLayout,
bindingListener: InverseBindingListener?) {
Log.e("swipeBinding","setOnRefreshingListener")
if (bindingListener != null)
swipeRefreshLayout.setOnRefreshListener {
bindingListener.onChange() //每當swipeRefreshLayout刷新狀態被用戶的
//操作改變,我們都能夠在這里監聽到,
//并交給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參數如下:
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 //一定要設置lifeCycleOwner 否則LiveData雙向綁定不起作用
viewModel.refreshing.observe(this, Observer {
Log.e("mainActivity2,","isRefreshing:${it}")
})
txt_hide.setOnClickListener {
viewModel.refreshing.postValue(false)
}
}
}
上面有個地方需要注意下:
當使用LiveData進行雙向綁定的時候 一定要記得調用binding.setLifeCycleOwner方法,否則LiveData數據改變的時候,View沒法收到通知,切記!!可以想象到binding內部使用這個LifecycleOwner給liveData設置了監聽。
下面就是這個方法的詳細說明:
/**
* 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.作用于方法,方法須為公共靜態方法。
2.方法的第一個參數必須為View類型,如TextView等
3.需要與@BindingAdapter配合使用
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
public @interface InverseBindingAdapter {
String attribute();
String event() default "";
}
attribute:String類型,必填,表示當值發生變化時,要從哪個屬性中檢索這個變化的值,示例:"android:text"
event: String類型,非必填;如果填寫,則使用填寫的內容作為event的值;如果不填,在編譯時會根據attribute的屬性名再加上后綴“AttrChanged”生成一個新的屬性作為event的值,舉個例子:attribute屬性的值為”android:text”,那么默認會在”android:text”后面追加”AttrChanged”字符串,生成”android:textAttrChanged”字符串作為event的值.
event屬性的作用: 當View的值發生改變時用來通知dataBinding值已經發生改變了。開發者一般需要使用@BindingAdapter創建對應屬性來響應這種改變
2)@InverseBindingMethod與@InverseBindingMethods
1.@InverseBindingMethods注解用于標記類
2.@InverseBindingMethod注解需要與@InverseBindingMethods注解結合使用才能發揮其功效
3.@InverseBindingMethods需要與@BindingAdapter配合使用才能發揮功效
用法與示列:
@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類型,非必填,屬性值的生成規則以及作用和@InverseBindingAdapter中的event一樣。
method:String類型,非必填,對于什么時候填什么時候不填,這里舉個例子說明:比如SeekBar,它有android:progress屬性,也有getProgress方法【你沒看錯,就是getProgress,不是setProgress】,所以對于SeekBar的android:progress屬性,不需要明確指定method,因為不指定method時,默認的生成規則就是前綴“get”加上屬性名,組合起來就是getProgress,而剛才也說了,getProgress方法在seekBar中是存在的,所以不用指定method也可以,但是如果默認生成的方法getXxx在SeekBar中不存在,而是其他方法比如getRealXxx,那么我們就需要通過method屬性,指明android:xxx對應的get方法是getRealXxx,這樣dataBinding在生成代碼時,就使用getRealXxx生成代碼了;從宏觀上來看,@InverseBindingMethod的method屬性的生成規則與@BindingMethod的method屬性的生成規則其實是類似的
3)@InverseMethod
作用于方法,
用于雙向綁定
@InverseMethod 注解是一個相對獨立的注解,不需要其他注解的配合就可以發揮它強大的作用,它的作用就是為某個方法指定一個相反的方法。它有一個String類型的必填屬性:value,用來存放與當前方法對應的相反方法
正方法與反方法的要求:
- 正方法與反方法的參數數量必須相同
- 正方法的最終參數的類型與反方法的返回值必須相同
- 正方法的返回值類型必須與反方法的最終參數類型相同。
用列如下:
@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()方法結合使用
該注解用于標記實體類中的get方法或“is”開頭的方法,且實體類必須繼承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 + ",是不是學生=" + isStudent;
}
}
@Bindable注解是用來干什么的?
使用@Bindable注解標記的get方法,在編譯時,會在BR類中生成對應的字段,然后與notifyPropertyChanged()方法配合使用,當該字段中的數據被修改時,dataBinding會自動刷新對應view的數據,而不用我們在拿到新數據后重新把數據在setText()一遍,就憑這一點,dataBinding就可以簡化大量的代碼