JetPack

https://mp.weixin.qq.com/s?__biz=MzAxMTI4MTkwNQ==&mid=2650826588&idx=1&sn=21288ece071c7c0d1ead1d4cd8a95c67&chksm=80b7b3c2b7c03ad4877d1204f27734b7b5f13990d2af774df5b96f699cc28778e43843d007b2&scene=0#rd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1. Android Jetpack 架构组件之 Lifecycle(使用篇)
https://blog.csdn.net/Alexwll/article/details/80638905

2. Android Jetpack 架构组件之 Lifecycle(源码篇)
https://blog.csdn.net/Alexwll/article/details/82491901

3. Android Jetpack 架构组件之 ViewModel (源码篇)
https://blog.csdn.net/Alexwll/article/details/82459614

4. Android Jetpack 架构组件之 LiveData(使用、源码篇)
https://blog.csdn.net/Alexwll/article/details/82996003

5. Android Jetpack架构组件之 Paging(使用、源码篇)
https://blog.csdn.net/Alexwll/article/details/83246201

6. Android Jetpack 架构组件之 Room(使用、源码篇)
https://blog.csdn.net/Alexwll/article/details/83033460

7. Android Jetpack 架构组件之Navigation
https://blog.csdn.net/Alexwll/article/details/83244004

8. Android Jetpack架构组件之WorkManger
https://blog.csdn.net/Alexwll/article/details/83244871

即学即用 Android Jetpack - ViewModel & LiveData - 简书

LiveData is an observable data holder class. Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services.

The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.

可以通过下面的图来看 ViewModelLiveData 之间的关系

还可以通过下面的代码来看 ViewModelLiveData 之间的关系

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class ShoeModel constructor(shoeRepository: ShoeRepository) : ViewModel() {

// 品牌的观察对象 默认观察所有的品牌
private val brand = MutableLiveData<String>().apply {
value = ALL
}

// 鞋子集合的观察类
val shoes: LiveData<List<Shoe>> = brand.switchMap {
// Room数据库查询,只要知道返回的是LiveData<List<Shoe>>即可
if (it == ALL) {
shoeRepository.getAllShoes()
} else {
shoeRepository.getShoesByBrand(it)
}
}

fun setBrand(brand:String){
this.brand.value = brand

this.brand.map {

}
}

fun clearBrand(){
this.brand.value = ALL
}

companion object {
private const val ALL = "所有"
}
}

验证 LiveData 的在两个 fragments 上共享数据

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// LoginLiveData
class LoginLiveData : MutableLiveData<LoginInfo>() {

companion object {
private lateinit var sInstance: LoginLiveData

@MainThread
fun get(): LoginLiveData {
sInstance = if (::sInstance.isInitialized) sInstance else LoginLiveData()
return sInstance
}
}
}

// LoginFragment.onViewCreated
Log.e("TTAG", "from LoginFragment, the init liveData is: ${LoginLiveData.get().value}")
LoginLiveData.get().observe(viewLifecycleOwner, Observer {
Log.e("TTAG", "from LoginFragment, the liveData changed: $it")
})
Handler().postDelayed({
LoginLiveData.get().apply {
value = LoginInfo("login", "login", "login@qq.com")
}
}, 2000)

// WelcomeFragment.onViewCreated
Log.e("TTAG", "from LoginFragment, the init liveData is: ${LoginLiveData.get().value}")
LoginLiveData.get().observe(viewLifecycleOwner, Observer {
Log.e("TTAG", "from LoginFragment, the liveData changed: $it")
})
Handler().postDelayed({
LoginLiveData.get().apply {
value = LoginInfo("login", "login", "login@qq.com")
}
}, 2000)

日志

1
2
3
4
5
6
7
# 首次进入welcome
E/TTAG: from WelcomeFragment, the init liveData is: null
E/TTAG: from WelcomeFragment, the liveData changed:LoginInfo(account=welcome, pwd=welcome, email=welcome@qq.com)
# 点击进入login
E/TTAG: from LoginFragment, the init liveData is: LoginInfo(account=welcome, pwd=welcome, email=welcome@qq.com)
E/TTAG: from LoginFragment, the liveData changed: LoginInfo(account=welcome, pwd=welcome, email=welcome@qq.com)
E/TTAG: from LoginFragment, the liveData changed: LoginInfo(account=login, pwd=login, email=login@qq.com)

Room

即学即用 Android Jetpack - Room - 简书

Android Room 框架学习 - 简书

Paging

问题: Jetpack Paging 闪烁

描述: 一点点的不相关的修改都会导致列表刷新,源码如下

1
2
3
4
5
6
7
8
9
10
companion object {
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<DbFlowDay>() {
override fun areItemsTheSame(oldItem: DbFlowDay, newItem: DbFlowDay): Boolean {
return oldItem === newItem
}
override fun areContentsTheSame(oldItem: DbFlowDay, newItem: DbFlowDay): Boolean {
return oldItem.day == newItem.day
}
}
}

解决,如下源码:

1
2
3
4
5
6
7
8
9
10
companion object {
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<DbFlowDay>() {
override fun areItemsTheSame(oldItem: DbFlowDay, newItem: DbFlowDay): Boolean {
return oldItem.id == newItem.id // !!! 这里,比较id,而不是对象
}
override fun areContentsTheSame(oldItem: DbFlowDay, newItem: DbFlowDay): Boolean {
return oldItem.day == newItem.day
}
}
}

databinding recyclerView

DataBinding 在 RecyclerView 中的使用 - 简书