Closed Berki2021 closed 3 years ago
viewBinding() requires to know exactly class of ViewBinding. I’ll make this improvements in next release
You need instance of Class if ViewBinding
Hmm okay, thank you for the fast reply! Is it possible to say, when the next release will be published? I am looking forward for this feature 👍
No. Right now you can do the next:
abstract class BaseEmailFragment<VB: ViewDataBinding>(
layout: Int,
vbClass: Class<VB>
) : Fragment(layout) {
private val binding: VB by viewBinding(FragmentViewBinder(VB::class.java)::bind)
}
class FragmentViewBinder<T : ViewBinding>(private val viewBindingClass: Class<T>) {
private companion object {
val bindViewMethod by lazy(LazyThreadSafetyMode.NONE) {
viewBindingClass.getMethod("bind", View::class.java)
}
}
fun bind(fragment: Fragment): T {
return bindViewMethod(null, fragment.requireView()) as T
}
}
Thanks I will try it out. But viewBindingClass.getMethod()
did not work when using it in combination with private companion object
. And in by viewBinding(FragmentViewBinder(VB::class.java)::bind)
it still says "Cannot use 'vb' as reified type parameter. Use a class instead"
Will be added in 1.3.0
I am trying to pass a generic type to the viewBinding Delegate, but it says:
Cannot use 'vb' as reified type parameter. Use a class instead
. Is this even possible?