5

I have a custom flip transformation in a ViewPager2, I've disabled user input so it can only change pages programmatically, however, the speed of the transition is way too fast.

The behavior I need is with 2 fragments, the first one loads and after a few milliseconds I trigger the transition programmatically, I see the animation and the 2nd fragment, that's it.

Here is the pager related code:

viewPager.apply {
    adapter = ViewPagerAdapter(this@HostFragment)
    setPageTransformer(VerticalFlipTransformation())
    isUserInputEnabled = false
}

At some point I trigger the transition like this:

viewPager.currentItem = 1

Here is my adapter:

private class ViewPagerAdapter(fragment: Fragment) :
    FragmentStateAdapter(fragment) {

    override fun getItemCount() = 2

    override fun createFragment(position: Int) = if (position == 0) {
        Fragment1()
    } else {
        Fragment2()
    }
}

Finally here is the transformation I'm using:

class VerticalFlipTransformation : ViewPager2.PageTransformer {

    override fun transformPage(page: View, position: Float) {
        page.translationX = -position * page.width
        page.cameraDistance = 20000f

        if (position < 0.5 && position > -0.5) {
            page.visibility = VISIBLE
        } else {
            page.visibility = GONE
        }

        when {
            position < -1 -> {
                page.alpha = 0f
            }
            position <= 0 -> {
                page.alpha = 1f
                page.rotationX = 180 * (1 - abs(position) + 1)
            }
            position <= 1 -> {
                page.alpha = 1f
                page.rotationX = -180 * (1 - abs(position) + 1)
            }
            else -> {
                page.alpha = 0f
            }
        }
    }
}

I need to slow down the transition, any ideas? Thanks in advance!

Alejandro Casanova
  • 3,633
  • 4
  • 31
  • 46

0 Answers0