Kotlin Android Extentions ViewBinding for RecyclerView ViewHolder (LayoutContainer)

Kotlin Android Extensions ViewBinding works by default for Activity and Fragment.

import kotlinx.android.synthetic.main.activity_main.*class TestActivity : Activity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)        // Instead of findViewById<TextView>(R.id.textView)        textView.setText("Hello world!")    }}

To make it work for ViewHolder in RecyclerView, use LayoutContainer.

Edit build.gradle (Module)

androidExtensions {
    // https://kotlinlang.org/docs/tutorials/android-plugin.html#experimental-mode
    experimental = true
}
class LocalListAdapter() : RecyclerView.Adapter<LocalListAdapter.ViewHolder>() {    override fun onBindViewHolder(holder: ViewHolder, position: Int) {        holder.apply {            textView.text = "Hello"        }    }    inner class ViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView), LayoutContainer}

NOTE: You can access view in ViewHolder by holder.itemView.textView (without LayoutContainer), but it doesn't comes with View Caching

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.