0

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?

tguen
  • 189
  • 1
  • 9
  • if you are using androidx import is `import androidx.databinding.ViewDataBinding;` – Madhawa Priyashantha Jun 10 '20 at 02:06
  • Unfortunately changing the import line does not solve the problem. It still says package does not exist. – tguen Jun 10 '20 at 02:23
  • @tguen Your code looks good. You may try to [re-check these tips](https://stackoverflow.com/questions/39483094/data-binding-class-not-generated/51579759#51579759) – NamNH Jun 10 '20 at 02:43

0 Answers0