You might be interested in these two articles:
On using data binding with RecyclerView:
https://medium.com/google-developers/android-data-binding-recyclerview-db7c40d9f0e4
On using data binding with lists without RecyclerView (e.g. LinearLayout):
https://medium.com/google-developers/android-data-binding-list-tricks-ef3d5630555e
With ItemDecoration, you must add your own BindingAdapter because RecyclerView allows multiple item decorations. Something like this should work:
@BindingAdapter("itemDecoration")
public static void setItemDecoration(RecyclerView view, ItemDecoration old,
ItemDecoration newVal) {
if (old != null) {
view.removeItemDecoration(old);
}
if (newVal != null) {
view.addItemDecoration(newVal);
}
}
Your question about Context is a little confusing. I'm trying to imagine how you would need Context in data binding. Data binding expressions do not allow new
so you wouldn't be able to create one that way. Perhaps you're thinking of using some representation instead:
@BindingAdapter("dividerDirection")
public static void setItemDecoration(RecyclerView view, int oldDirection, int newDirection) {
if (oldDirection != newDirection) {
ItemDecoration decoration =
new DividerItemDecoration(view.getContext(), newDirection);
ItemDecoration old = ListenerUtil.trackListener(view, decoration, R.id.decoration);
if (old != null) {
view.removeItemDecoration(old);
}
view.addItemDecoration(decoration);
}
}
and it would be bound like this:
<android.support.v7.widget.RecyclerView
app:dividerDirection="@{DividerItemDecoration.HORIZONTAL}" .../>
For other uses, you are automatically granted a built-in "context" variable in your layout and you can pass it to any methods you call. It is the Context of the root View of the bound view hierarchy and should work for most of your needs. You should not need to pass the context in the model for most uses.
I expect that should also answer your question about ItemAnimator, though you don't need a special BindingAdapter to use an attribute since it has a setter already:
<android.support.v7.widget.RecyclerView
app:itemAnimator="@{model.animator}" .../>