第一步:引入sticky-layoutmanager库
第二步:设置layoutManager
recyclerView.run {
adapter = mAdapter
layoutManager = StickyHeadersLinearLayoutManager<ContentAdapter>(requireContext())
}
第三步:adapter 实现 StickyHeaders接口,isStickyHeader方法确定谁为header。
class ContentAdapter :BaseMultiItemQuickAdapter<XXXBean,BaseViewHolder>(),
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)
}
网友评论