1

I had big hopes to use the new databinding support on Android to finally get rid of all the boilerplate associated with RecyclerViews, only to find out that the topic is barely mentioned on the official Android databinding docs.

So even thou I found a couple of blog posts with 'tips' on the subject, I'm still looking for a full example implementation on how to avoid having to create an adapter per recyclerview instance.

Here's some reference code, but it's not complete: https://stfalcon.com/en/blog/post/faster-android-apps-with-databinding#takeaways

JayJay
  • 123
  • 1
  • 10
  • 1
    Assuming that you mean "avoid having to create an adapter class per distinct data set and rendering rule set for use in a `RecyclerView`", that's not going to be possible except in limited circumstances. The `RecyclerView.Adapter` is the class that knows where your data is coming from; the code for that will vary by representation of that data (e.g., `ArrayList` vs. `[]` vs. `Cursor`). The `Adapter` subclass also deals with things like multiple view types and data insertions/removals, which are outside the scope of the data binding framework. – CommonsWare Aug 07 '16 at 20:37
  • You can find the full `RecyclerView.Adapter` in the sample app the blog writers have pushed to [GitHub](https://github.com/stfalcon-studio/DataBindingExample). – yennsarah Aug 08 '16 at 05:30
  • Thank you Amylinn, but looks like the example you linked is for two-way databinding, which is not the use case for recyclerviews. – JayJay Aug 08 '16 at 12:52
  • This was the project for the blog article you mentioned in your question. You have a complete code example on the `RecyclerView` and its usage? – yennsarah Aug 09 '16 at 05:41
  • The UniqueAdapter With DataBinding --> http://stackoverflow.com/a/39553323/703225 – qinmiao Sep 20 '16 at 07:18

1 Answers1

1

There is great solution using delegates - http://hannesdorfmann.com/android/adapter-delegates I use it approach with simple changes for DataBinding.

If you are looking for simple way, look at this library https://github.com/drstranges/DataBinding_For_RecyclerView

Any way, even if you are using common way, magic is in bindable ViewHolder:

public class BindingHolder<VB extends ViewDataBinding> extends RecyclerView.ViewHolder {

    private VB mBinding;

    public static <VB extends ViewDataBinding> BindingHolder<VB> newInstance(
            @LayoutRes int layoutId, LayoutInflater inflater,
            @Nullable ViewGroup parent, boolean attachToParent) {

        VB vb = DataBindingUtil.inflate(inflater, layoutId, parent, attachToParent);
        return new BindingHolder<>(vb);
    }

    public BindingHolder(VB binding) {
        super(binding.getRoot());
        this.mBinding= binding;
    }

    public VB getBinding() {
        return mBinding;
    }
}

in onCreateViewHolder

{
    BindingHolder holder = BindingHolder.newInstance(R.layout.item,
            LayoutInflater.from(parent.getContext()), parent, false);
    //set listeners and action handlers
    return holder;
}

in onBindViewHolder

{
    ItemBinding binding = holder.getBinding();
    Item item = items.get(position);
    binding.setItem(item);
    binding.executePendingBindings();
}
// or 
{
    ViewDataBinding binding = holder.getBinding();
    Object item = items.get(position);
    binding.setVariable(BR.item, item);
    binding.executePendingBindings();
}
Roman_D
  • 4,680
  • 2
  • 14
  • 17