androidandroid-recyclerviewepoxy

Android EpoxyRecyclerView - switching orientation


I am trying to build the following layout structure with Epoxy:

enter image description here

First three layouts are from EpoxyAModel. The fourth one is from the EpoxyBModel.

This is my buildModels function:

override fun buildModels() {

    aModels.forEach { //this happens 3 times
        modelA {
            id("someID")
            (...)

        }
    }

    modelB {
        id("someID2")
        (...)
    }

}

With this set up I am getting the following result:

enter image description here

This is understandable, since the documentation of EpoxyRecyclerView says:

If the RecyclerView is set to match_parent size then the scrolling orientation is set to vertical and setHasFixedSize is set to true.

If the height is set to wrap_content then the scrolling orientation is set to horizontal, and setClipToPadding is set to false for carousel usage.

My question is: is there a possibility of getting the desired effect or do I have to define another EpoxyRecyclerView and controller?

Thanks in advance.


Solution

  • Okay, I figured it out!

    Firstly, we need to assign a GridLayoutManager to the EpoxyRecyclerView:

    recyclerView.layoutManager = GridLayoutManager(context, 3)
    

    Then, inside the EpoxyBModel we need to override the getSpanSize method to tell the layout manager that this element is as big as a whole row of items:

    override fun getSpanSize(totalSpanCount: Int, position: Int, itemCount: Int): Int = 3