Android Weekly Issue #467
Jetpack Compose: Styles and Themes (Part II)
Puppy App的theme設(shè)置.
改狀態(tài)欄, 在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使用總結(jié)
從ViewModel暴露數(shù)據(jù)到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 { ... }
}
}
}
其他的組合都會保持上游的活躍, 浪費(fèi)資源.
- 用
WhileSubscribed
暴露屬性, 在lifecycleScope.launch/launchWhenX
里收集. - 通過
Lazily/Eagerly
暴露, 用repeatOnLifecycle
收集.
推薦閱讀.
Gradle: Version Catalogs
寫一個依賴的文件.
然后用法類似于這樣:
implementation(libs.androidx.core.ktx)