For example java code
public abstract class BindingElement<T extends ViewDataBinding> {
T binding;
abstract public T createBinding(LayoutInflater inflater, ViewGroup parent);
public BindingElement(ViewGroup parent) {
binding = createBinding(LayoutInflater.from(parent.getContext()), parent);
binding.setLifecycleOwner(ViewTreeLifecycleOwner.get(parent));
}
}
I need some necessary property that defined in constructor. And then i will do something with that property. What is the best way write it in kotlin?
This doesn’t directly answer your question but provides a safer alternative.
You should avoid calling an open or abstract function from the constructor in Java or Kotlin, even though it’s allowed. It is fragile and can cause weird bugs that are difficult to resolve. Read here: In Java, is there a legitimate reason to call a non-final method from a class constructor?
An alternative in this case would be to make your function into a constructor parameter. Your class doesn’t even need to be open or abstract to support this.
class ViewBindingParameter<T: ViewBindingData> (
parent: ViewGroup,
inflateBinding: (LayoutInflater, ViewGroup)->T
) {
val binding: T = inflateBinding(LayoutInflater.from(parent.context), parent)
}
Usage:
val bindingParam = ViewBindingParameter(parent, SomeBinding::inflate)
If you aren't planning to add features to this class, you might as well just use a function that directly returns a binding so you don't have to deal with the wrapper class. Maybe an extension function of the parent view:
fun <T: ViewBindingData> ViewGroup.inflateChildBinding(inflateBinding: (LayoutInflater, ViewGroup)->T): T =
inflateBinding(LayoutInflater.from(context), this)
and use it like:
val binding = parent.inflateChildBinding(SomeBinding::inflate)