Android Weekly Notes

Android Weekly Notes #467

2021-07-17  本文已影响0人  圣骑士wind

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的两种方式:

  1. 暴露一个StateFlow属性, 用WhileSubscribed加上一个timeout.
class MyViewModel(...) : ViewModel() {
    val result = userId.mapLatest { newUserId ->
        repository.observeItem(newUserId)
    }.stateIn(
        scope = viewModelScope, 
        started = WhileSubscribed(5000), 
        initialValue = Result.Loading
    )
}
  1. repeatOnLifecycle收集.
onCreateView(...) {
    viewLifecycleOwner.lifecycleScope.launch {
        viewLifecycleOwner.lifecycle.repeatOnLifecycle(STARTED) {
            myViewModel.myUiState.collect { ... }
        }
    }
}

其他的组合都会保持上游的活跃, 浪费资源.

推荐阅读.

Gradle: Version Catalogs

写一个依赖的文件.
然后用法类似于这样:

 implementation(libs.androidx.core.ktx)

Code

上一篇下一篇

猜你喜欢

热点阅读