Kotlin Android 扩展是否缓存合成属性或每次调用 findViewById() 时?

Do Kotlin Android Extensions cache the synthetic properties or each time it calls findViewById()?

如果我有一个简单的自定义视图:

myitem.xml

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
<FrameLayout/>

访问 kotlinx 语法 属性:

import kotlinx.android.synthetic.main.myitem.view.*

view.toolbar.text = "Some text"

它在内部生成对 findByViewID() 的调用。所以我的问题是:

是否为自定义视图(如活动)或每次调用 findByViewID 时缓存结果?出于性能原因,答案非常重要。

在当前版本 (1.1.3) 中,为 Activity 和 Fragments 布局缓存了视图。对于 RecyclerView ViewHolders 等其他类型的容器,没有缓存。

此外,缓存是一个 HashMap,对键进行整数装箱。 SparseArray 会更好。

编辑:从 1.1.4 版开始,如果您让它们实现 LayoutContainer 接口,也可以为其他 类 缓存视图,包括 ViewHolder。您还可以使用 @ContainerOptions 注释来指定另一个缓存实现,包括 SparseArray。这两个功能仍处于实验阶段,需要在您的 build.gradle 文件中手动启用:

androidExtensions {
    experimental = true
}

Read more关于它。

自 1.1.4 以来,视图可以缓存在任何 class 中。 默认启用自定义视图缓存。对于 ViewHolder,您需要像这样实现 LayoutContainer 接口:

class MyViewHolder(override val containerView: View): LayoutContainer

有关详细信息,请参阅此文档 https://github.com/Kotlin/KEEP/blob/master/proposals/android-extensions-entity-caching.md

更新: 为了能够使用 LayoutContainer,您应该将其添加到 gradle 脚本中:

androidExtensions {
    experimental = true
}