Kotlin Android扩展缓存合成属性还是每次调用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 syntentic属性:

 import kotlinx.android.synthetic.main.myitem.view.* view.toolbar.text = "Some text" 

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

结果缓存为自定义视图像活动或每次每次调用findByViewID被调用? 性能方面的原因,答案非常重要。

由于1.1.4视图可以缓存在任何类中。 在默认启用的自定义视图中进行缓存。 对于ViewHolders,您需要像这样实现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 }

在当前版本(1.1.3)中,视图被缓存为活动和片段布局。 对于其他种类的容器,如RecyclerView ViewHolders,没有缓存。

此外,缓存是一个HashMap与Integer拳击键。 SparseArray会更好。

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

 androidExtensions { experimental = true } 

阅读更多关于它。