MVVM封装
- 一.MVVM
- 1.依赖
- 2.mvvm封装
- 3.业务层调用
- 1.MainService
- 2.MainRepository
- 3.MainViewModel
- 4.TestActivity
- 4.独立运行main业务模块
一.MVVM
1.依赖
config.gradle
//jetpack
def lifecycle_version = "2.5.1"
libLivedata = "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
libViewmodel = "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
libFragmentKtx = "androidx.fragment:fragment-ktx:1.5.5"
//room
def room_version = "2.4.0"
libRoom = "androidx.room:room-runtime:$room_version"
libRoomCompiler = "androidx.room:room-compiler:$room_version"//room kotlin注解
libRoomKtx ="androidx.room:room-ktx:$room_version"
libRoomRxjava = "androidx.room:room-rxjava2:$room_version"
libRoomPaging = "androidx.room:room-paging:$room_version"
//hilt依赖注入
libHilt = 'com.google.dagger:hilt-android:2.46'
libHiltCompiler = 'com.google.dagger:hilt-android-compiler:2.46'
base:build.gradle
//jetpack
api libLivedata
api libViewmodel
api libFragmentKtx
//room
// api libRoom
// kapt libRoomCompiler //注解
// api libRoomKtx
// api libRoomRxjava
// api libRoomPaging
//hilt依赖注入
// api libHilt
// kapt libHiltCompiler
注意:所有的build.gradle配置,
dataBinding{
enabled = true
}
2.mvvm封装
open class BaseRepository {
/**
* 创建对应的service api接口
*/
fun <T> createApiService(service:Class<T>):T{
return RetrofitManager.getRetrofit().create(service)
}
}
open class BaseViewModel:ViewModel() {
//所有ViewModel公共方法
}
/**
* 1。沉浸式状态栏
* 2。DataBinding findViewById
* 3。initData():数据
* 4.initListener():点击事件
* 5。EventBus事件总线封装
* 6.数据加载中,数据加载失败,数据加载成功,无网络等
*
*/
abstract class BaseActivity<VDB:ViewDataBinding>:AppCompatActivity() {
protected lateinit var viewDataBinding:VDB
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewDataBinding = DataBindingUtil.setContentView(this,getLayout())//initView
initListener()
initData()
}
abstract fun getLayout():Int //返回布局id
abstract fun initData() //初始化数据
abstract fun initListener()//事件监听
}
abstract class BaseMVVMActivity<VM:BaseViewModel,VDB:ViewDataBinding>:BaseActivity<VDB>() {
protected val viewModel:VM by lazy { ViewModelProvider(this).get(getJVMClass()) }
//子类重写 返回具体的viewmodel对应class文件
abstract fun getJVMClass():Class<VM>
}
3.业务层调用
1.MainService
interface MainService {
@GET("/banner/json")
fun banner():LiveData<ApiResponse<MutableList<BannerEntity>>>
}
2.MainRepository
class MainRepository:BaseRepository() {
//使用MainService请求轮播图数据
fun banner():LiveData<ApiResponse<MutableList<BannerEntity>>>{
return createApiService(MainService::class.java).banner()
}
}
3.MainViewModel
class MainViewModel:BaseViewModel() {
private var mainResponse = MainRepository()//仓库
fun banner(): LiveData<ApiResponse<MutableList<BannerEntity>>> {
return mainResponse.banner()
}
}
4.TestActivity
class TestActivity : BaseMVVMActivity<MainViewModel,MainActivityTestBinding>() {
override fun getLayout(): Int {
return R.layout.main_activity_test
}
override fun initData() {
viewModel.banner()
.observe(this){
viewDataBinding.tv.text = it.data.toString()
}
}
override fun initListener() {
viewDataBinding.tv.setOnClickListener {
ToastUtils.showShort("hahah")
}
}
override fun getJVMClass(): Class<MainViewModel> {
return MainViewModel::class.java
}
}
4.独立运行main业务模块
改为true
配置独立运行的清单文件