With data binding, we have a DataBindingUtil class which has an inflate method that can handle any layout id:
binding = DataBindingUtil.inflate<ViewDataBinding>(inflater, layoutId, parent, false)
With view binding, there doesn't appear to be a ViewBindingUtil class. Inflate methods are meant to be called on concrete binding types. But what if you don't know the concrete binding type ahead of time? One solution is running a when statement on the layoutId:
binding = when (layoutId) {
R.id.layout1 -> Layout1Binding.inflate(...)
R.id.layout2 -> Layout2Binding.inflate(...)
R.id.layout3 -> Layout3Binding.inflate(...)
etc...
}
But that's a lot of code compared to the one liner of data binding. Is there a better way? I found this 3rd party lib but it looks like it uses reflection: https://github.com/matsudamper/ViewBindingUtil