Skip to content

Instantly share code, notes, and snippets.

@radekkozak
Forked from gmk57/1 ViewBindingDelegates.kt
Created November 9, 2021 23:03
Show Gist options
  • Save radekkozak/d8e74e7fe3aa5173bc994cdbfcd4052a to your computer and use it in GitHub Desktop.
Save radekkozak/d8e74e7fe3aa5173bc994cdbfcd4052a to your computer and use it in GitHub Desktop.
Kotlin delegates for Android View Binding with usage examples
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.viewbinding.ViewBinding
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
/** Activity binding delegate, may be used since onCreate up to onDestroy (inclusive) */
inline fun <T : ViewBinding> AppCompatActivity.viewBinding(crossinline factory: (LayoutInflater) -> T) =
lazy(LazyThreadSafetyMode.NONE) {
factory(layoutInflater)
}
/** Fragment binding delegate, may be used since onViewCreated up to onDestroyView (inclusive) */
fun <T : ViewBinding> Fragment.viewBinding(factory: (View) -> T): ReadOnlyProperty<Fragment, T> =
object : ReadOnlyProperty<Fragment, T>, DefaultLifecycleObserver {
private var binding: T? = null
override fun getValue(thisRef: Fragment, property: KProperty<*>): T =
binding ?: factory(requireView()).also {
// if binding is accessed after Lifecycle is DESTROYED, create new instance, but don't cache it
if (viewLifecycleOwner.lifecycle.currentState.isAtLeast(Lifecycle.State.INITIALIZED)) {
viewLifecycleOwner.lifecycle.addObserver(this)
binding = it
}
}
override fun onDestroy(owner: LifecycleOwner) {
binding = null
}
}
/** Binding delegate for DialogFragments implementing onCreateDialog (like Activities, they don't
* have a separate view lifecycle), may be used since onCreateDialog up to onDestroy (inclusive) */
inline fun <T : ViewBinding> DialogFragment.viewBinding(crossinline factory: (LayoutInflater) -> T) =
lazy(LazyThreadSafetyMode.NONE) {
factory(layoutInflater)
}
/** Not really a delegate, just a small helper for RecyclerView.ViewHolders */
inline fun <T : ViewBinding> ViewGroup.viewBinding(factory: (LayoutInflater, ViewGroup, Boolean) -> T) =
factory(LayoutInflater.from(context), this, false)
class MainActivity : AppCompatActivity() {
private val binding by viewBinding(ActivityMainBinding::inflate)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.button.text = "Bound!"
}
}
// Don't forget to pass layoutId in Fragment constructor
class RegularFragment : Fragment(R.layout.fragment) {
private val binding by viewBinding(FragmentBinding::bind)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.button.text = "Bound!"
}
}
// DialogFragment with onCreateDialog doesn't have a view lifecycle, so we need a different delegate
class DialogFragment1 : DialogFragment() {
private val binding by viewBinding(FragmentBinding::inflate)
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
binding.button.text = "Bound!"
return AlertDialog.Builder(requireContext()).setView(binding.root).create()
}
}
// For DialogFragment with full-blown view we can use a regular Fragment delegate (actually the
// whole code here is exactly the same as in RegularFragment)
// NB: Constructor with layoutId was only recently added (in Fragment 1.3.0)
class DialogFragment2 : DialogFragment(R.layout.fragment) {
private val binding by viewBinding(FragmentBinding::bind)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.button.text = "Bound!"
}
}
// For RecyclerView we don't need any delegates, just a property.
// Unfortunately, here we have a name overloading: View Binding vs "binding" holder to data (onBindViewHolder).
// ViewGroup.viewBinding() helper function can reduce boilerplate a little.
class Adapter1 : ListAdapter<String, Adapter1.Holder>(Differ()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
return Holder(parent.viewBinding(ListItemBinding::inflate))
}
override fun onBindViewHolder(holder: Holder, position: Int) {
holder.binding.textView.text = getItem(position)
}
class Holder(val binding: ListItemBinding) : RecyclerView.ViewHolder(binding.root)
private class Differ : DiffUtil.ItemCallback<String>() { ... }
}
// Alternatively, we can use generic BoundHolder for all Adapters
class Adapter2 : ListAdapter<String, BoundHolder<ListItemBinding>>(Differ()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BoundHolder<ListItemBinding> {
return BoundHolder(parent.viewBinding(ListItemBinding::inflate))
}
override fun onBindViewHolder(holder: BoundHolder<ListItemBinding>, position: Int) {
holder.binding.textView.text = getItem(position)
}
private class Differ : DiffUtil.ItemCallback<String>() { ... }
}
open class BoundHolder<T : ViewBinding>(val binding: T) : RecyclerView.ViewHolder(binding.root)
// Personally, I prefer to encapsulate view creation & manipulation inside ViewHolder.
// In this case BoundHolder can be used as a superclass.
class Adapter3 : ListAdapter<String, Adapter3.Holder>(Differ()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = Holder(parent)
override fun onBindViewHolder(holder: Holder, position: Int) = holder.bind(getItem(position))
class Holder(parent: ViewGroup) : BoundHolder<ListItemBinding>(parent.viewBinding(ListItemBinding::inflate)) {
fun bind(item: String) {
binding.textView.text = item
}
}
private class Differ : DiffUtil.ItemCallback<String>() { ... }
}
abstract class BoundHolder<T : ViewBinding>(protected val binding: T) : RecyclerView.ViewHolder(binding.root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment