Lorenzo Quiroli
1 min readApr 24, 2017

--

Thanks! Well, it’s a bit more complicated for a Fragment since you’d have to keep a reference to the rootview. So you might declare a lateinit var root : View, save the reference of the rootview in the onCreateView and finally use this extension method:

fun <T : View> Fragment.bind(root: View, @IdRes res : Int) : Lazy<T> {
@Suppress("UNCHECKED_CAST")
return lazy(LazyThreadSafetyMode.NONE){ root.findViewById(res) as T }
}

It’s not an ideal solution but it should work

--

--