0

我正在使用 Groupie Adapter 将我的数据填充到 RecylerView,其中包含不同类型的项目并使用可扩展的组和部分,其中一个项目是 Horizo​​ntal RecyclerView - 一切正常,除了 ** 我无法将子 Horizo​​ntal RecyclerView 滚动到查看它的项目。**


    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

PiHorizo​​ntalListItem 包含一个 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)
        }
    }
}

任何人都可以帮我解决这个问题,或者是否有解决方法?

4

1 回答 1

0

我找到了解决方案,我用一个 Group 包裹了我的 PiHorizo​​ntalListItem 然后将它添加到适配器,现在它工作正常。谢谢

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

以及轮播的外观以及如何处理列表视图

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)
    }
}
于 2022-01-17T07:53:29.273 回答