Android Weekly Notes #405

作者: 圣骑士wind | 来源:发表于2020-03-16 14:52 被阅读0次

Android Weekly Issue #405

Glide'ing your way into RecyclerView state invalidation

在RecyclerView中显示list, 图像item, 点击查看大图.

但是无法保存位置?

保存位置是可以自动的额, 只要确保:

  • RecyclerView有id.
  • Adapter中的数据在第一次layout之前就有了.

解决方案: 缓存数据.

但是问题似乎还是没有解决, 为什么呢? -> 初始状态确实是保存并恢复了, 但是之后又被invalidated了.

线索: RecyclerView的尺寸被child决定时.

Glide缓存图片时用的是原始尺寸. 在把图片放进ImageView里时会做计算. 但是xml里面写的wrap_content, 所以Glide会对每个图片进行计算, 而在此时RecyclerView可能先做完了尺寸计算, image默认高度为0, 直到后来被加入图像.

尺寸计算影响了可见item的计算, 从而让RecyclerView重置了状态, 影响了状态恢复.

解决方法:

  • 在xml里写死高度.
  • Glide设定override(Target.SIZE_ORIGINAL, 200).
  • 用一个placeholder.

Handling Nullability in Android 11 and Beyond

Android 11的preview:
https://developer.android.com/preview

给API加了更多的注解:
@RecentlyNullable and @RecentlyNonNull to @Nullable and @NonNull.

前两个是warnings, 后两个是errors.

8 Valid Reasons for Modularization in Android

模块化的8个好处.

Improving App Debugging With Flipper

Facebook开发的stetho的后代: Flipper: https://fbflipper.com/

比起Stetho的优势: 增加了API, 支持各种自定义扩展.

Combining Kotlin Flows with Select Expressions

多个Flow怎么结合? 有多种操作符可以选择.

实验特性: select expression

Coroutines: First things first

协程的取消和异常.

Collections in Kotlin

Kotlin集合.

Easy Android Scopes

保持对象的几种方法:

  • Activity的onRetainNonConfigurationInstance()中返回现有的presenter.
  • Fragment的setRetainInstance().
  • AndroidX ViewModel.
  • scoped delegate: 一种代理的方式, 把viewmodel又包了一下. 这样更灵活, 可以绑定任何类型的对象.

Smart Casts via Assertions + Kotlin Contracts

新学了一个词: Rubber duck debugging.

这段代码:

fun main() {
    val str: String? = “”
    str.assertNotNull()
    println(“string length = ${str.length}”) // will not compile
}

其中assert那句并不能让后面的语句默认变量已经不为null了.

需要用到Kotlin的Contracts(还是实验阶段):

@UseExperimental(ExperimentalContracts::class)
fun String?.assertNotNull() {
    contract {
        returns() implies (this@assertNotNull != null)
    }
    if (this == null){
        throw AssertionError()
    }
}

Preventing coroutine cancellation for important actions

协程还可以阻止取消: NonCancellable.

Scripting file templates for Android Studio

创建文件模板: https://medium.com/codequest/file-group-templates-in-android-studio-unofficial-guide-85dfa0a0c1ec

本文分享的脚本, 把文件模板拷贝到repo里.

Modern User Storage on Android

Android 10和11的文件存储.

Codes

arkivanov/MVIKotlin

MVI framework.

Videos

Google Design Tutorials

设计课程, youtube playlist.

相关文章

网友评论

    本文标题:Android Weekly Notes #405

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