Kotlin Android Extensions是否会缓存合成属性或每次调用findViewById()?

Win*_*der 8 android kotlin kotlin-android-extensions

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

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/>
Run Code Online (Sandbox Code Playgroud)

访问kotlinx syntentic属性:

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

view.toolbar.text = "Some text"
Run Code Online (Sandbox Code Playgroud)

在内部,它会生成一个调用findByViewID().所以我的问题是:

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

Bla*_*der 8

在当前版本(1.1.3)中,将为"活动"和"片段"布局缓存视图.对于像RecyclerView ViewHolders这样的其他类型的容器,没有缓存.

此外,缓存是一个HashMap带有Integer boxing for keys 的缓存.A SparseArray本来会更好.

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

androidExtensions {
    experimental = true
}
Run Code Online (Sandbox Code Playgroud)

阅读更多相关信息.


dev*_*ant 6

由于 1.1.4 视图可以缓存在任何类中。默认情况下启用自定义视图中的缓存。对于 ViewHolders,您需要实现这样的LayoutContainer接口:

class MyViewHolder(override val containerView: View): LayoutContainer
Run Code Online (Sandbox Code Playgroud)

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

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

androidExtensions {
    experimental = true
}
Run Code Online (Sandbox Code Playgroud)