1 build.gradle(:app) 添加ViewBinding & ViewDataBinding支持
android {
...
buildFeatures {
dataBinding true
viewBinding true
}
...
}
2 写基类 BaseActivity
package com.jhj.mymvvm.base
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
/**
* @author 刘建波
* 3/7/22
* 描述:Activity 基类
*/
abstract class BaseActivity<Binding : ViewBinding> : AppCompatActivity() {
lateinit var mBinding: Binding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mBinding = bindFactory()
performBusinessLogic()
}
/**
* 子类决定去绑定 ViewBinding 抑或 ViewDataBinding
*/
abstract fun bindFactory(): Binding
/**
* 绑定成功,执行业务逻辑
*/
abstract fun performBusinessLogic()
}
public abstract class ViewDataBinding extends BaseObservable implements ViewBinding
我们可以看到,ViewDataBinding 实现了 ViewBinding 接口,换句话说,二者都是 ViewBinding,根据这个我们可以在基类中用泛型去做了。基类中只负责抽象,让子类去负责实现。
2.1 MainActivity(实现ViewBinding的子类)
package com.jhj.mymvvm
import android.view.View
import com.jhj.mymvvm.base.BaseActivity
import com.jhj.mymvvm.base.helper.JumpHelper
import com.jhj.mymvvm.databinding.ActivityMainBinding
class MainActivity : BaseActivity<ActivityMainBinding>(), View.OnClickListener {
/**
* [BaseActivity] 的 ViewBinding 实例
*/
override fun bindFactory(): ActivityMainBinding {
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
return binding
}
override fun performBusinessLogic() {
mBinding.tvDataBinding.text = "[BaseActivity] 的 ViewBinding 实例"
mBinding.tvDataBinding.setOnClickListener(this)
}
override fun onClick(view: View?) {
when (view?.id) {
R.id.tvDataBinding -> {
val intent = Intent(this, DataBindingActivity::class.java)
startActivity(intent)
}
}
}
}
对应的 activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvDataBinding"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:background="@color/b_blue"
android:gravity="center"
android:text="DataBinding"
android:textColor="@color/white"
android:textSize="20sp" />
</LinearLayout>
2.2 DataBindingActivity(实现ViewDataBinding的子类)
package com.jhj.mymvvm.base.databinding
import androidx.databinding.DataBindingUtil
import com.jhj.mymvvm.R
import com.jhj.mymvvm.base.BaseActivity
import com.jhj.mymvvm.databinding.ActivityDataBindingBinding
class DataBindingActivity : BaseActivity<ActivityDataBindingBinding>() {
/**
* [BaseActivity] 的 ViewDataBinding 实例
*/
override fun bindFactory(): ActivityDataBindingBinding {
return DataBindingUtil.setContentView(this, R.layout.activity_data_binding)
}
override fun performBusinessLogic() {
mBinding.content = "[BaseActivity] 的 ViewDataBinding 实例"
}
}
对应的 activity_data_binding.xml文件
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="content"
type="java.lang.String" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvDataBinding"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:background="@color/b_blue"
android:gravity="center"
android:text="@{content}"
android:textColor="@color/white"
android:textSize="20sp" />
</LinearLayout>
</layout>