在使用BRVAH库基础上三步实现sticky header

sticky-layoutmanager

第一步:引入sticky-layoutmanager库

第二步:设置layoutManager

recyclerView.run {
            adapter = mAdapter
            layoutManager = StickyHeadersLinearLayoutManager(requireContext())
        }

第三步:adapter 实现 StickyHeaders接口,isStickyHeader方法确定谁为header。

class ContentAdapter :BaseMultiItemQuickAdapter(),
    LoadMoreModule, StickyHeaders {
    init {
        addItemType(Header, R.layout.item_header)
        addItemType(Content, R.layout.item_content)
    }

    override fun getItemCount() = data.size
    override fun convert(holder: BaseViewHolder, item: XXXBean) {
        when (holder.itemViewType) {
            Header -> {
               ...
            }
            Content -> {
                ...
            }
        }
    }

    override fun isStickyHeader(position: Int): Boolean {
        return data[position].itemType == Header
    }
}

组装list

val groupBy = mutableListOf.groupBy { xxxBean: XXXBean ->
                    条件...
                }

groupBy.forEach { _, list ->
    val sticky= XXXBean(header)
    listData.add(sticky)
    listData.addAll(list)
}

你可能感兴趣的:(在使用BRVAH库基础上三步实现sticky header)