效果
計(jì)算可滑動(dòng)百分比
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)
}
})
分頁(yè)
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) {//同一頁(yè)
genMultiTypeItem(total, item, arrayList)
} else {//不相等 開始分頁(yè)
newList.addAll(listOf(arrayList)) // 上一頁(yè) 添加
arrayList = ArrayList()
lastGroupIndex = groupIndex
genMultiTypeItem(total, item, arrayList)
}
if (total - 1 == index) {//last
newList.addAll(listOf(arrayList))
}
}
return newList
}