我正在尝试根据recyclerview项中数据的条件更改Recyclerview中每个viewHolder项的spanCount。当前,我正在使用位置更改spanCount。但是,如何根据位置以外的条件更改每个Items(Viewholder)的跨度?例如,我想做if (type == Item.type) return 1
一些例子或提示会很可爱!我希望收到您的来信!
gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return if (adapter.isMainHead(position) && tabIndex == 0) {
gridLayoutManager.spanCount
} else {
1
}
}
}
答案 0 :(得分:1)
希望此示例对您有帮助。
(layoutManager as GridLayoutManager).setSpanSizeLookup(object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
when (adapter.getItemViewType(position)) {
CalendarAdapter.TYPE_HEADER -> return 7
CalendarAdapter.TYPE_ITEM -> return 1
CalendarAdapter.TYPE_SPAN -> return dataArrayList[position].myVar.toInt()
else -> return -1
}
}
})