一花颗、前言:
最近在學習DataBinding的使用捕传,中間遇到了不少的坑,記錄以下扩劝,幫助以后學習DataBinding的朋友庸论。
//啟動dataBinding
dataBinding {
enabled = true
}
二职辅、使用:
有一個 ViewModel 類,有一個 xml聂示,xml 綁定了 ViewModel .
1域携、@{(view) -> viewModel.click1(view,viewModel.key)}
//xml
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#55FF55"
android:gravity="center"
android:text="按鈕1"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{(view) -> viewModel.click1(view,viewModel.key)}"
/>
//viewModel
fun click1(view: View,msg:String){
Log.d(TAG,"======哈哈哈,點擊click1======${msg}")
}
2鱼喉、@{(view) -> viewModel.click2(view)}
//xml
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#55FF55"
android:baselineAligned="false"
android:gravity="center"
android:text="按鈕2"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{(view) -> viewModel.click2(view)}"
/>
//viewModel
fun click2(view: View){
Log.d(TAG,"======哈哈哈秀鞭,點擊click2======")
if (view is TextView){
view.setText("按鈕2的值變化了")
}
}
3、@{() -> viewModel.click3()}
//xml
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#55FF55"
android:gravity="center"
android:text="按鈕3"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{() -> viewModel.click3()}"
/>
//viewModel
fun click3(){
Log.d(TAG,"======哈哈哈蒲凶,點擊click3======")
}
4气筋、@{viewModel::click4}
//xml
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#55FF55"
android:gravity="center"
android:text="按鈕4"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{viewModel::click4}"
/>
//viewModel
fun click4(view: View){
Log.d(TAG,"======哈哈哈,點擊click4======")
}
5旋圆、@{handler::clickEventHandler}
//xml
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#55FF55"
android:gravity="center"
android:text="按鈕5"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{handler::clickEventHandler}"
/>
//EventHandlers
class EventHandlers {
val TAG = "LUO"
// EventHandlers 執(zhí)行click事件
fun clickEventHandler(view: View){
Log.d(TAG,"======哈哈哈,點擊clickEventHandler======")
}
}
6麸恍、@{() -> viewModel.click6(viewModel.Key)}
//xml
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#55FF55"
android:gravity="center"
android:text="按鈕6"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{() -> viewModel.click6(viewModel.Key)}"
/>
//viewModel
fun click6(value:String){
Log.d(TAG,"======哈哈哈灵巧,點擊click6======${value}")
}
7、@{() -> viewModel.click7(100)}
//xml
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#55FF55"
android:gravity="center"
android:text="按鈕7"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{() -> viewModel.click7(100)}"
/>
//viewModel
fun click7(value:Int){
Log.d(TAG,"======哈哈哈抹沪,點擊click7======${value}")
}
8刻肄、@{() -> activity.run2()}
//xml
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#55FF55"
android:gravity="center"
android:text="按鈕8"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{() -> activity.run2()}"
/>
//TwoActivity
class TwoActivity :AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_two)
}
fun run2() {
Log.d("LUO","=======TwoActivity點擊事件測試=====")
}
}
9、@{(view)->viewModel.click9(100,view)}
//xml
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#55FF55"
android:gravity="center"
android:text="按鈕9"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{(view)->viewModel.click9(100,view)}"
/>
//viewModel
fun click9(value:Int,view :View){
Log.d(TAG,"======哈哈哈融欧,點擊click9======${value}")
}
三敏弃、完整示例:
1、MainActivity
class MainActivity : AppCompatActivity() {
private val viewModel: MainViewModel by lazy { ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory(application)).get(MainViewModel::class.java) }
@Override
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(
this@MainActivity,
R.layout.activity_main
)
//綁定生命周期
binding.lifecycleOwner = this
//綁定viewModel
binding.viewModel = viewModel
//讓其它對象綁定MainActivity
val handlers = EventHandlers()
binding.handler = handlers
binding.activity = TwoActivity()
//調(diào)用viewModel方法
viewModel.context = this
viewModel.updateTime()
}
}
2噪馏、MainViewModel
/**
* 數(shù)據(jù)處理
*/
class MainViewModel(application: Application) : AndroidViewModel(application) {
var context : Activity? = null
val TAG = "LUO"
val Key = ObservableField<String>("測試")
init {
Key.set("***歡迎光臨***")
}
/**
* 更新時間
*/
fun updateTime() {
Log.d(TAG,"======哈哈哈麦到,時間更新了======")
}
fun click1(view: View,msg:String){
Log.d(TAG,"======哈哈哈,點擊click1======${msg}")
}
fun click2(view: View){
Log.d(TAG,"======哈哈哈欠肾,點擊click2======")
if (view is TextView){
view.setText("按鈕2的值變化了")
}
}
fun click3(){
Log.d(TAG,"======哈哈哈瓶颠,點擊click3======")
}
fun click4(view: View){
Log.d(TAG,"======哈哈哈,點擊click4======")
}
fun click6(value:String){
Log.d(TAG,"======哈哈哈刺桃,點擊click6======${value}")
}
fun click7(value:Int){
Log.d(TAG,"======哈哈哈粹淋,點擊click7======${value}")
}
fun click9(value:Int,view :View){
Log.d(TAG,"======哈哈哈,點擊click9======${value}")
}
fun click10(){
Log.d(TAG,"======點擊click10,進入封裝的MVVM頁面======")
context?.startActivity(Intent(context,NewMainActivity::class.java))
}
}
3瑟慈、activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="viewModel"
type="com.dotc.kotlinmvvmdemo.viewModel.MainViewModel" />
<variable
name="handler"
type="com.dotc.kotlinmvvmdemo.EventHandlers" />
<variable
name="activity"
type="com.dotc.kotlinmvvmdemo.TwoActivity" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#55FF55"
android:gravity="center"
android:text="按鈕1"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{(view) -> viewModel.click1(view,viewModel.key)}"
/>
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#55FF55"
android:baselineAligned="false"
android:gravity="center"
android:text="按鈕2"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{(view) -> viewModel.click2(view)}"
/>
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#55FF55"
android:gravity="center"
android:text="按鈕3"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{() -> viewModel.click3()}"
/>
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#55FF55"
android:gravity="center"
android:text="按鈕4"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{viewModel::click4}"
/>
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#55FF55"
android:gravity="center"
android:text="按鈕5"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{handler::clickEventHandler}"
/>
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#55FF55"
android:gravity="center"
android:text="按鈕6"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{() -> viewModel.click6(viewModel.Key)}"
/>
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#55FF55"
android:gravity="center"
android:text="按鈕7"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{() -> viewModel.click7(100)}"
/>
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#55FF55"
android:gravity="center"
android:text="按鈕8"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{() -> activity.run2()}"
/>
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#55FF55"
android:gravity="center"
android:text="按鈕9"
android:textColor="#FF0000"
android:textSize="25sp"
android:onClick="@{(view)->viewModel.click9(100,view)}"
/>
<TextView
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#F5555F"
android:gravity="center"
android:text="進入封裝的MVVM頁面"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:onClick="@{()->viewModel.click10()}"
/>
</LinearLayout>
</layout>
4桃移、EventHandlers
class EventHandlers {
val TAG = "LUO"
// EventHandlers 執(zhí)行click事件
fun clickEventHandler(view: View){
Log.d(TAG,"======哈哈哈,點擊clickEventHandler======")
}
}
5葛碧、TwoActivity
class TwoActivity :AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_two)
}
fun run2() {
Log.d("LUO","=======TwoActivity點擊事件測試=====")
}
}
四借杰、注意事項:
1、項目完整地址:https://gitee.com/lyyon/KotlinMvvmDemo
2吹埠、這個項目包含Activity和Fragment的完整MVVM封裝第步;