Android Weekly Issue #467
Jetpack Compose: Styles and Themes (Part II)
Puppy App的theme设置.
改状态栏, 在theme里:
<item name="statusBarBackground">@color/grey</item>
<item name="android:statusBarColor" tools:targetApi="l">?attr/statusBarBackground</item>
<item name="android:navigationBarColor">?android:attr/windowBackground</item>
view raw
EXPLORING THE MATERIAL NAVIGATION RAIL
竖直放置的navigation bar.
<com.google.android.material.navigationrail.NavigationRailView
android:id="@+id/navigation_rail"
app:headerLayout="@layout/layout_rail"
... />
Migrating from LiveData to Kotlin’s Flow
从LiveData迁移到Flow, 几种情况的代码举例.
还讨论了StateFlow的stateIn和收集操作符.
StateFlow使用总结
从ViewModel暴露数据到UI, 用StateFlow
的两种方式:
- 暴露一个StateFlow属性, 用
WhileSubscribed
加上一个timeout.
class MyViewModel(...) : ViewModel() {
val result = userId.mapLatest { newUserId ->
repository.observeItem(newUserId)
}.stateIn(
scope = viewModelScope,
started = WhileSubscribed(5000),
initialValue = Result.Loading
)
}
- 用
repeatOnLifecycle
收集.
onCreateView(...) {
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.lifecycle.repeatOnLifecycle(STARTED) {
myViewModel.myUiState.collect { ... }
}
}
}
其他的组合都会保持上游的活跃, 浪费资源.
- 用
WhileSubscribed
暴露属性, 在lifecycleScope.launch/launchWhenX
里收集. - 通过
Lazily/Eagerly
暴露, 用repeatOnLifecycle
收集.
推荐阅读.
Gradle: Version Catalogs
写一个依赖的文件.
然后用法类似于这样:
implementation(libs.androidx.core.ktx)
网友评论