airbnb / epoxy

Epoxy is an Android library for building complex screens in a RecyclerView
https://goo.gl/eIK82p
Apache License 2.0
8.52k stars 728 forks source link

Nested EpoxyRecyclerViews with Databinding & Kotlin #825

Open Chaseos opened 5 years ago

Chaseos commented 5 years ago

I'm attempting to have a EpoxyRecyclerView of cards, and within them a EpoxyRecyclerView of names, but I'm getting a NPE crash when doing so on the item's recycler.

My Parent view has a EpoxyRecyclerView and my cardItem that I insert in it has a EpoxyRecyclerView.

I'm using the .withModels extension and the auto generated models through making the package-info.java class.

When I try to call .withModels on the item's EpoxyRecyclerView I get a NPE.

So it's pretty much like this. There will never be more than 5 or so card items.

binding.parentsEpoxyRecycler.withModels {
            cardsList.forEachIndexed { index, card ->
                cardView {
                    id(index)
                    card.names.forEachIndexed { index, name ->
                        itemEpoxyRecycler.withModels { // <- NPE here
                            nameView {
                                id(index + 10)
                            }
                        }
                    }
                }
            }
Chaseos commented 5 years ago

So, after working with it a bit and with a teammate's help, we found a solution that works. Basically involves just creating a BindingAdapter for the second EpoxyRecyclerView.

binding.parentsEpoxyRecycler.withModels {
    cardsList.forEachIndexed { index, card ->
        cardView {
            id("card", index.toLong())
            names(card.names)
        }
    }
}

@BindingAdapter("listOfNames")
fun EpoxyRecyclerView.setupNamesRecyclerView(names: List<String>) {
    withModels {
        names.forEachIndexed { index, name ->
            nameView {
                id("name", index.toLong())
                name(name)
            }
        }
    }
}

Then in your EpoxyRecyclerView for the cards you set the listOfNames value to the names you passed in.

elihart commented 5 years ago

Great, I'm not too familiar with databinding but I'm glad you have it working.