I enabled view binding in my gradle file and began using it (with LayoutNameBinding classes), including in RecyclerViews. All of my recycler adapters' ViewHolder classes just held a view binding object, so I tried making a generic ViewHolder. In android documentation, ViewDataBinding says "Base class for generated data binding classes." So I figured I could do this:
abstract class ViewBindingHolder<T extends ViewDataBinding> extends RecyclerView.ViewHolder {
T views;
public ViewBindingHolder(@NonNull T views) {
super(views.getRoot());
this.views = views;
}
}
This should allow me to do the following in my recycler adapter:
@Override @NonNull
public ViewBindingHolder<MyLayoutBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
return new ViewBindingHolder(MyLayoutBinding.inflate(getLayoutInflater(), viewGroup, false));
}
@Override
public void onBindViewHolder(@NonNull ViewBindingHolder<MyLayoutBinding> vbh, int position) {
vbh.views.myTextView.setText("...");
}
Unfortunately this isn't working. In the ViewBindingHolder class, I get the error "cannot resolve symbol ViewDataBinding" but no option to import it. I even tried manually adding import android.databinding.ViewDataBinding;
but the compiler says the package does not exist. How do I actually use this class?