2

I have a nested recycler view(a parent and several childs), I also use data binding.

The problem is that, since the adapter for nested recyclers is different, I do not know how to set the nested recyclers adaptor in Kotlin or Java.

enter image description here

In case I don't want to use data binding, the solution in this link is working fine.

Thanks

Ali ZahediGol
  • 876
  • 2
  • 10
  • 20

2 Answers2

1

Databinding is data driving,so you can place the child adapter in the parent model, and define a databinding adapter for recyclerview.

pulic class ParentModel
{
    private ChildAdapter childAdapter;
}
public class DataBindingAdapters
{
    @BindingAdapter({"adapter"})
    public void setRecyclerViewAdapter(RecyclerView recyclerView, ChildAdapter childAdapter)
    {
        recyclerView.serAdapter(childAdapter);
    }
}
<recyclerView
    app:adapter="@{model.childAdapter}"/>
zhoumy
  • 11
  • 2
0

To set RecyclerView adapter using android data binding.

Layout code:

Data part:

<data>
    <variable
        name="adapter"
        type="com.app.adapter.RecyclerViewAdapter" />
</data>

Recyclerview Layout:

<androidx.recyclerview.widget.RecyclerView
  android:id="@+id/recycler_view"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  app:adapter="@{adapter}"
  app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
  android:orientation="vertical"/>

Java file: DataBindingAdapters.java

public class DataBindingAdapters {

    @BindingAdapter({"adapter"})
    public static void setRecyclerViewAdapter(RecyclerView recyclerView,
                                        RecyclerViewAdapter recyclerViewAdapter) {

        recyclerView.setAdapter(RecyclerViewAdapter);
    }
}

Note:
1. setRecyclerViewAdaptermust be static to avoid this error.
2. In my code RecyclerViewAdapter is the custom recyclerview adapter.
3. Set LayoutManager either in code or in layout file, but not in both.(Xml would be preferred when using android data binding)
4. Refer this SO post for queries related to the layout manager like how to set for androidX, orientation, gridview rows, etc.

Abhimanyu
  • 11,351
  • 7
  • 51
  • 121