Share
Embed
findViewById
Introduction to the Kotlin Android Extensions.
by Juliane Lehmann
presented on October 26, 2017 at @Magicline
Kotlin User Group Hamburg
October 26, 2017
Transcript
17 slides
-
The plain way lateinit var titleText: TextView lateinit var empty:
View fun onCreate(...) { titleText = findViewById(R.id.title_text) empty = findViewById(R.id.empty) } Access as: titleText empty Walks the view hierarchy to find view with matching id -
The butterknife way @BindView(R.id.title_text) lateinit var titleText: TextView @BindView(R.id.empty) lateinit
var empty: View Butterknife.bind(this) Access as: titleText empty Generated injector code (“Butterknife is like a dagger, only infinitely less sharp”) Can also bind listeners, and other resources -
The databinding way import my.project.databinding.MainActivityBinding private lateinit var viewBinding: MainActivityBinding
viewBinding = DataBindingUtil.setContentView(this, R.layout.activity_main) Access as: viewBinding.title_text viewBinding.empty But mostly, instead, use databinding how it is actually meant (access data object via DSL from layout XML). -
The Kotlin Android Extensions way import kotlinx.android.synthetic.main.activity_main.* Access as title_text
empty Also: import kotlinx.android.synthetic.main.view_listitem.view.* fun bindItem(...) { itemView.title_text ... itemView.empty … } -
Simple magic explained Reminder: extension properties exist file ListItemLayout: val
View.title_text: TextView get() = this.findViewById<TextView>(R.id.title_text) -
Simple magic explained Reminder: extension properties exist file ListItemLayout: val
View.title_text: TextView inline get() = this.findViewById<TextView>(R.id.title_text) -
Simple magic explained Reminder: extension properties exist file ListItemLayout: val
View.title_text: TextView inline get() = this.findViewById<TextView>(R.id.title_text) This is basically what you get with import kotlinx.android.synthetic.main.activity_main.view.* -
Simple magic explained Reminder: extension properties exist file ListItemLayout: val
View.title_text: TextView inline get() = this.findViewById<TextView>(R.id.title_text) This is basically what you get with import kotlinx.android.synthetic.main.activity_main.view.* Great for view holders, (fragments) -
More magic import kotlinx.android.synthetic.main.activity_main.* (without view) instead inlines accessing a
generated cache … which gets automatically cleared in Fragment.onDestroyView -
More magic import kotlinx.android.synthetic.main.activity_main.* (without view) instead inlines accessing a
generated cache … which gets automatically cleared in Fragment.onDestroyView Great for activities, custom views, (fragments) - with experimental flag, also works for any class implementing kotlinx.android.extensions.LayoutContainer -
The crib sheet apply plugin: ‘kotlin-android-extensions’ For containers - Activity,
Fragment, View, (LayoutContainer): import kotlinx.android.synthetic.$buildType.$layout_file_name.* (cached extension properties on the container) For inlined access on child views: import kotlinx.android.synthetic.$buildType.$layout_file_name.view.* (extension properties on View) -
Links • Sample project on github: https://github.com/strooooke/KotlinAndroidExtensionsExample • codegen part
of Kotlin Android Extensions plugin: https://github.com/JetBrains/kotlin/tree/7da6ff78c10692b46a25596fb8ad256 51ee4dfd2/plugins/android-extensions/android-extensions-compiler/src/org/je tbrains/kotlin/android/synthetic/codegen