Search code examples
androidkotlinandroid-recyclerviewgroupie

Horizontal RecyclerView as an Item in Groupie Adapter does not work


I am populating my data to the RecylerView using Groupie Adapter with different types of items and using expandable groups and sections, one of the items is a Horizontal RecyclerView - everything works fine except that ** I am not able to scroll the child Horizontal RecyclerView to see its items.**


    var uiList = mutableListOf<Group>()
    private val adapter = GroupAdapter<GroupieViewHolder>()
    ......
    val horizontalListItem = PiHorizontalListItem(
        mediaIdCounter++,
        horizontalAdapter,
        nestedSharedViewPool

    )

    expandableGroup.add(
        horizontalListItem
    )
    .....
    uiList.plusAssign(expandableGroup)

    adapter.addAll(uiList)
    mainRecyclerView.adapter = adapter

where the PiHorizontalListItem contains a RecyclerView

class PiHorizontalListItem(
    private val id: Long,
    private val carouselAdapter: GroupAdapter<GroupieViewHolder>,
    private val sharedPool: RecyclerView.RecycledViewPool
) :Item() {

    override fun getId(): Long = id

    override fun getLayout(): Int = R.layout.item_horizontal_rv

    override fun bind(viewHolder: GroupieViewHolder, position: Int) {
        viewHolder.containerView.rv.apply {

            setRecycledViewPool(sharedPool)
            (layoutManager as LinearLayoutManager).orientation = LinearLayoutManager.HORIZONTAL
            (layoutManager as LinearLayoutManager).recycleChildrenOnDetach = true
            adapter = carouselAdapter
            setHasFixedSize(true)
        }
    }
}

Can anybody help me fix this issue or if there is a workaround for it?


Solution

  • I found the solution I wrapped my PiHorizontalListItem with a Group then add it to the adapter and now it is working fine. Thanks

    var uiList = mutableListOf<Group>()
    private val adapter = GroupAdapter<GroupieViewHolder>()
    .......
    val carousel = CarouselGroup(mediaIdCounter++,horizontalAdapter,nestedSharedViewPool)
    carouselSection.add(carousel)
    expandableGroup.add(carouselSection) 
    .....
    uiList.plusAssign(expandableGroup)
        
    adapter.addAll(uiList)
    mainRecyclerView.adapter = adapter
    

    and here how the carousel looks like and how to handles the listview

    class CarouselGroup(id:Long, madapter: GroupAdapter<GroupieViewHolder>,  nestedSharedViewPool : RecyclerView.RecycledViewPool,) :
        Group {
        private var isEmpty = true
        private val sharedPool = nestedSharedViewPool
        private val adapter = madapter
        private var groupDataObserver: GroupDataObserver? = null
        private var carouselItem = PiHorizontalListItem(id, adapter,sharedPool)
    
        private val adapterDataObserver: AdapterDataObserver = object : AdapterDataObserver() {
    
            override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
                val empty = adapter.itemCount == 0
                if (empty && !isEmpty) {
                    isEmpty = empty
                    groupDataObserver!!.onItemRemoved(carouselItem, 0)
                }
            }
    
            override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
                val empty = adapter.itemCount == 0
                if (isEmpty && !empty) {
                    isEmpty = empty
                    groupDataObserver!!.onItemInserted(carouselItem, 0)
                }
            }
        }
    
        override fun getItemCount(): Int {
            return if (isEmpty) 0 else 1
        }
    
        override fun getItem(position: Int): Item<*> {
            return if (position == 0 && !isEmpty) carouselItem else throw IndexOutOfBoundsException()
        }
    
        override fun getPosition(item: Item<*>): Int {
            return if (item === carouselItem && !isEmpty) 0 else -1
        }
    
        override fun registerGroupDataObserver(groupDataObserver: GroupDataObserver) {
            this.groupDataObserver = groupDataObserver
        }
    
        override fun unregisterGroupDataObserver(groupDataObserver: GroupDataObserver) {
            this.groupDataObserver = null
        }
    
        init {
            carouselItem = PiHorizontalListItem(id, adapter,sharedPool)
            isEmpty = adapter.itemCount == 0
            adapter.registerAdapterDataObserver(adapterDataObserver)
        }
    }