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

Viewed 1835

If I have a simple custom view:

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/>

Accessing a kotlinx syntentic property:

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

view.toolbar.text = "Some text"

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

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.

2 Answers

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

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

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

androidExtensions {
    experimental = true
}
Related