Igor Klimov Igor Klimov - 3 months ago 32
Android Question

Android Data Binding Binding Adapter with class reference

I created this Binding Adapter

public static void setAdapter(RecyclerView view, RecyclerView.Adapter<RecyclerView.ViewHolder> adapter) {

and set in XML


it gives all kinds of errors with it. What I want is to do the same thing as you can do with LayoutManager in XML like this


Do you know how to solve this issue with passing a class?

P.S. I don't want to pass a variable reference in XML, because let's say I want to use only XML.


It appears that you're trying to set a class in the XML, but are accepting an instance in your BindingAdapter. If you want to accept a class and instantiate it in your BindingAdapter, it will use reflection, like this:

public static void setAdapter(RecyclerView view, Class adapter)  {
    try {
        Object instance = adapter.newInstance();
    } catch (Exception e) {

If you want to pass an instance to your binding (a better approach, IMO), you don't need a BindingAdapter. You can rely on the existing setAdapter call:

        <variable name="adapter" type="android.support.v7.widget.RecyclerView.Adapter"/>
        app:adapter="@{adapter}" .../>