Reputation: 12318
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.
Upvotes: 10
Views: 1957
Reputation: 3711
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
}
Upvotes: 6
Reputation: 12939
In the current version (1.1.3), views are cached for Activities and Fragments layouts. For other kinds of containers like RecyclerView ViewHolders, there is no cache.
Also, the cache is a HashMap
with Integer boxing for keys. A SparseArray
would have been better.
Edit: Since version 1.1.4, views can be cached for other classes too, including ViewHolder
, if you make them implement the LayoutContainer
interface. You can also use the @ContainerOptions
annotation to specify another cache implementation, including SparseArray
. Both of these features are still experimental and need to be enabled manually in your build.gradle
file:
androidExtensions {
experimental = true
}
Read more about it.
Upvotes: 8