RecyclerView 跟隨指示器滑動

效果

image.png

計算可滑動百分比

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
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容