0

When using CompositePageTransformer to decrease the size of side contents of ViewPager, if the current index is removed, the new current item maintains the decreased size from when it was on the side, but since it is now on center, its size needs to be reseted. How can this behavior be achieved without requesting a fakeDrag/touch/scroll from user? Example:

enter image description here

The logic from the CompositePageAdapter on viewPager:

binding.vp.clipToPadding = false
binding.vp.clipChildren = false
binding.vp.getChildAt(0).overScrollMode = RecyclerView.OVER_SCROLL_NEVER
val compositePageTransformer = CompositePageTransformer()
compositePageTransformer.addTransformer(MarginPageTransformer(20)
compositePageTransformer.addTransformer { page, position ->
    val r = 1 - abs(position)
    page.scaleY = 0.85f + r * 0.15f
}
binding.vp.setPageTransformer(compositePageTransformer)

The function to remove an item:

val index = binding.vp.currentItem
contents.removeAt(index)
if (contents.isNotEmpty()) binding.vp.offscreenPageLimit = contents.size
binding.vp.adapter?.notifyItemRemoved(index)
if (contents.size >= index) {
    binding.vp.currentItem = index
} else if (contents.isNotEmpty()) {
    binding.vp.currentItem = index - 1
}
/* This is what i dont want to use */
Runnable {
    handler.postDelayed({
        binding.vp.beginFakeDrag() 
        binding.vp.fakeDragBy(100f)
        binding.vp.endFakeDrag()
    }, 100)
}.run()

QuickNotes: I've tried using removingTransformer/setPageTransformer(null)/creating a disabled CompositePageTransformer, yet they either had no changes or made the animations broken.

What actually helped a little was using fakeDragBy inside a handler after removing the item from the list, but it still has a wrong behavior on some specific scenarios and a flicker on the animation.

E.Akio
  • 2,249
  • 3
  • 14
  • 27

0 Answers0