MVVM簡介
MVVM分為Model、View、ViewModel,它們的作用分別為:
- Model:簡稱資料模型,包括從服務端獲取的json資料,bean,或者從本地獲取的資料等等;
- View:UI視圖,負責資料的顯示(activity,fragment,控制元件view等等);
- ViewModel:就是與界面(view)對應的Model,View與Model通過ViewModel實作資料的雙向系結,進行資料的系結和邏輯的代碼的實作
MVVM 的優點
- 主要目的是分離視圖(View)和模型(Model)
- 降低代碼耦合,提高視圖或者邏輯的重用性,
- 提高了模塊的可測驗性
下面參考一個自己寫的模仿登錄demo,來讓大家體驗一下MVVM,先來張效果圖:

注意:
1、在guidle里添加databinding
dataBinding {
enabled = true
}

2、在布局里面要使用 標簽
代碼
MainActivity
class MainActivity : AppCompatActivity(), IBaseView {
lateinit var mPersonVM: LoginViewModel
var mBinding: ActivityMainBinding? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
mPersonVM = LoginViewModel(this)
bt_login.setOnClickListener {
mPersonVM.getFristData(et_account.text.toString(), et_password.text.toString())
}
}
override fun setData(personBean: PersonBean) {
mBinding?.personBean = personBean//賦值之后就可以在textview中顯示出來
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="com.example.mvvmkotlin_wy.R" />
<variable
name="personBean"
type="com.example.mvvmkotlin_wy.bean.PersonBean" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="帳號:1,密碼:1,登錄成功\n其他:登錄失敗"
app:layout_constraintBottom_toTopOf="@+id/et_account"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入帳號"
app:layout_constraintBottom_toTopOf="@id/et_password"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入密碼"
app:layout_constraintBottom_toTopOf="@id/bt_login"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_account" />
<Button
android:id="@+id/bt_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登錄"
app:layout_constraintBottom_toTopOf="@id/tv_status"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_password" />
<TextView
android:id="@+id/tv_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{personBean.status}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bt_login" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
其他詳細的代碼可以下載demo來看:https://download.csdn.net/download/wy313622821/12822265
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/4430.html
標籤:其他
