目录
LiveData在数据发生变化的时候通知给观察者。
基本使用
kotlin
class MyViewModel(counter: Int) : ViewModel() {
val count = MutableLiveData<Int>()
init {
count.value = counter
}
fun add() {
val c = count.value ?: 0
count.value = c + 1
}
}
count变量是一个MutableLiveData对象,指定类型为Int。
MutableLiveData有三种读写数据的方法,getValue()、 setValue()、postValue().getValue()用于获取livedata中包含的数据。setValue()用于给LiveData设置数据,只能在主线中。postValue()用于在非主线程中给LiveData设置数据。
kotlin
class ClickActivity : ComponentActivity() {
lateinit var viewModel: MyViewModel
lateinit var binding: ActivityClickBinding
lateinit var sp: SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
binding = ActivityClickBinding.inflate(layoutInflater)
setContentView(binding.root)
sp = getSharedPreferences("data", MODE_PRIVATE)
val countSp = sp.getInt("count", 0)
viewModel =
ViewModelProvider(this, MyViewModelFactory(countSp)).get(MyViewModel::class.java)
binding.clickButC.setOnClickListener {
viewModel.add()
}
viewModel.count.observe(this, Observer { count ->
binding.clickTvText.text = count.toString()
})
}
override fun onPause() {
super.onPause()
sp.edit {
putInt("count", viewModel.count.value ?: 0)
}
}
}
点击一次增加一,直接使用了viewmodel的add方法。viewModel.count.observe来观察数据的变化,当viewmodel中count包含的数据发生变化,viewModel.count.observe就会被调用。
viewModel.count.observe还可以这么写
kotlin
viewModel.count.observe(this) { c ->
binding.clickTvText.text = c.toString()
}
修改viewmodel,只暴露不可变的LiveData给外部。代码如下
kotlin
class MyViewModel(counter: Int) : ViewModel() {
val count: LiveData<Int>
get() = countLD
private val countLD = MutableLiveData<Int>()
init {
countLD.value = counter
}
fun add() {
val c = countLD.value ?: 0
countLD.value = c + 1
}
}
将原来的count 变量改名为countLD变量,加上private修饰符,countLD对于外部就不可见了。新定义一个count变量,类型为不可变的LiveData,并在它的get()属性方法中返回countLD变量。当外部调用count变量时,获得是countLD 的实例,但无法给countLD 设置数据。
map和switchMap
map()的使用
数据格式转换:将一种数据类型转换为另一种,例如将Date转换为String,将实体转换为UI模型等。
条件判断与计算:根据原始数据计算衍生数据,例如根据用户生日计算年龄,根据商品数量和单价计算总价等。
数据过滤与加工:从原始数据中提取需要的信息,或者对数据进行加工,例如从用户对象中只取出姓名,或者将数字格式化为货币字符串。
有一个user类
kotlin
class User(val id: Int, val name: String, age: Int)
只需要名字
kotlin
class UserViewModel : ViewModel() {
private val userLiveData = MutableLiveData<User>()
val userName: LiveData<String> = userLiveData.map {
it.name
}
}
当userLiveData 的数据发生变化时,map()方法会监听到变化并执行转换函数中的逻辑,将转换后的数据赋值给userName的观察者。
map转换是同步的,因此不适合执行耗时操作。如果需要进行异步操作应该使用switchMap或者结合其他异步方式。
switchMap()的使用
用于将一个LiveData对象转换为另一个LiveData对象,并且当原始LiveData发出新值时,它会取消之前正在运行的转换并开始一个新的转换。
使用场景:
当你有这样一个LiveData,它的值变化会触发一个新的异步操作(例如网络请求或数据库查询),并且你只关心最近一次操作的结果时,可以使用switchMap。
避免在数据变化时产生多个同时运行的异步任务,只保留最后一个。
kotlin
class User2ViewModel : ViewModel() {
private val currentUserId = MutableLiveData<Long>()
val user2 = currentUserId.switchMap {
//模拟获取用户数据
UserRepository().getUserProfile(it)
}
fun switchUser(userId: Long) {
currentUserId.value = userId
}
}
UserRepository
kotlin
class UserRepository{
fun getUserProfile(l: Int): LiveData<User> {
return MutableLiveData<User>(User(l,"zzzzz",15))
}
}
activity
kotlin
viewModel =
ViewModelProvider(this).get(User2ViewModel::class.java)
binding.clickButC.setOnClickListener {
val uid = (0..1000).random()
viewModel.switchUser(uid)
}
viewModel.user2.observe(this) { user ->
binding.clickTvText.text = user.id.toString()
}
传入的userId值设置到currentUserId 中,currentUserId 数据发生变化,switchMap ()方法就会执行并调用转换函数, UserRepository().getUserProfile(it)方法会返回一个LiveData对象,switchMap 会将这个对象转换成一个可观察的LiveData对象。
