package com.gh.gamecenter.common.view

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.PageTransformer

/**
 * 从ViewPager2源码中摘抄，与ScrollEventListener搭配，实现在RecyclerView中使用PageTransformer
 * @see ScrollEventListener
 */
class PageTransformerAdapter(
    private val layoutManager: LinearLayoutManager,
    private val pageTransformer: PageTransformer
) : ViewPager2.OnPageChangeCallback() {

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
        val transformOffset = -positionOffset
        for (i in 0 until layoutManager.childCount) {
            val v = layoutManager.getChildAt(i) ?: return
            val currPos = layoutManager.getPosition(v)
            val viewOffset = transformOffset + (currPos - position)
            pageTransformer.transformPage(v, viewOffset)
        }
    }
}