效果
计算可滑动百分比
mRecyclerViewList?.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val totalWidth = itemWidth * (mAdapter?.data?.size?:0)
val canScrollWidth = totalWidth - (context.getScreenWidth())
offsetDx += dx
onScrollListener?.invoke(offsetDx / canScrollWidth)
}
})
分页
fun processKingKongData(list: List<AdvertItem>?): MutableList<ArrayList<AdvertItem>> {
if (list.isNullOrEmpty()) return mutableListOf()
val total = list.size
val newList = mutableListOf<ArrayList<AdvertItem>>()
var lastGroupIndex = 0
var arrayList = ArrayList<AdvertItem>()
list.forEachIndexed { index, item ->
val groupIndex = index / 5
if (lastGroupIndex == groupIndex) {//同一页
genMultiTypeItem(total, item, arrayList)
} else {//不相等 开始分页
newList.addAll(listOf(arrayList)) // 上一页 添加
arrayList = ArrayList()
lastGroupIndex = groupIndex
genMultiTypeItem(total, item, arrayList)
}
if (total - 1 == index) {//last
newList.addAll(listOf(arrayList))
}
}
return newList
}