Skip to content

Instantly share code, notes, and snippets.

@fleficher
Last active January 25, 2022 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fleficher/8759c4666aea291e281c2a1cbcc6c49a to your computer and use it in GitHub Desktop.
Save fleficher/8759c4666aea291e281c2a1cbcc6c49a to your computer and use it in GitHub Desktop.
Fragment View Binding Delegate
class FragmentAutoClearedValueBinding<T : ViewBinding>(
val binder: (View) -> T
) : ReadOnlyProperty<Fragment, T>,
DefaultLifecycleObserver {
private var value: T? = null
override fun onDestroy(owner: LifecycleOwner) {
value = null // Clear reference.
}
override fun getValue(thisRef: Fragment, property: KProperty<*>): T {
return value ?: binder(thisRef.requireView()).also {
setValue(thisRef, it)
}
}
private fun setValue(fragment: Fragment, value: T) {
fragment.viewLifecycle.removeObserver(this)
this.value = value
fragment.viewLifecycle.addObserver(this)
}
}
fun <T : ViewBinding> Fragment.viewBindingWithBinder(
binder: (View) -> T
) = FragmentAutoClearedValueBinding(binder)
@RaoulNL
Copy link

RaoulNL commented Jan 25, 2022

@dav-true indeed, that solves the problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment