一、前言:
最近在学习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 执行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()
//调用viewModel方法
viewModel.context = this
viewModel.updateTime()
}
}
2、MainViewModel
/**
* 数据处理
*/
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 执行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封装;