美文网首页Android Weekly Notes
Android Weekly Notes #467

Android Weekly Notes #467

作者: 圣骑士wind | 来源:发表于2021-07-17 23:58 被阅读0次

    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 { ... }
            }
        }
    }
    

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

    • WhileSubscribed暴露属性, 在lifecycleScope.launch/launchWhenX里收集.
    • 通过Lazily/Eagerly暴露, 用repeatOnLifecycle收集.

    推荐阅读.

    Gradle: Version Catalogs

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

     implementation(libs.androidx.core.ktx)
    

    Code

    相关文章

      网友评论

        本文标题:Android Weekly Notes #467

        本文链接:https://www.haomeiwen.com/subject/ibigpltx.html