Kotlin Android扩展会缓存合成属性还是每次调用findViewById()时缓存它们? [英] Do Kotlin Android Extensions cache the synthetic properties or each time it calls findViewById()?

查看:61
本文介绍了Kotlin Android扩展会缓存合成属性还是每次调用findViewById()时缓存它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I have a simple custom view:

myitem.xml

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协同属性:

Accessing a kotlinx syntentic property:

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

view.toolbar.text = "Some text"

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

Internally it generates a call to findByViewID(). So my question is:

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

Is the result cached for custom views like for activities or each each time findByViewID is called? The answer is quite important for performance reasons.

推荐答案

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

Since 1.1.4 views can be cached in any class. Caching in custom views enabled by default. For ViewHolders you need to implement LayoutContainer interface like this:

class MyViewHolder(override val containerView: View): LayoutContainer

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

See this doc for details https://github.com/Kotlin/KEEP/blob/master/proposals/android-extensions-entity-caching.md

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

Update: To be able to use LayoutContainer you should add this to the gradle script:

androidExtensions {
    experimental = true
}

这篇关于Kotlin Android扩展会缓存合成属性还是每次调用findViewById()时缓存它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆