3

I'm using the new Viewpager2 with a recycler view to show a list of pages. To create a stack like view I'm using ViewPager2.PageTransformer

I've noticed that according to the documentation for the ViewPager2.setPageTransformer() it stats the following

Note: setting a ViewPager2.PageTransformer disables data-set change animations to prevent conflicts between the two animation systems. Setting a null transformer will restore data-set change animations.

So after adding the page transformer I animation for page removing and page adding is no more working

Is there any way to overcome this

Below is the implemented code

Page Transformer


public class VerticalStackViewPageTransformer implements ViewPager2.PageTransformer {
    int pageTranslationY ;

    public VerticalStackViewPageTransformer(int pageTranslationY) {
        this.pageTranslationY = pageTranslationY;
    }

    @Override
    public void transformPage(@NonNull View page, float position) {
        page.setTranslationY(-pageTranslationY * position);
        page.setScaleX(1 - (0.15f * abs(position)));
        page.setAlpha(0.65f + (1 - abs(position)));
    }
}

Usage On Activity


 listViewPager.setAdapter(adapter);
 listViewPager.setOffscreenPageLimit(2);
 listViewPager.setPageTransformer(new VerticalStackViewPageTransformer(ContextHelper.dpToPixel(30,context)));

Ismail Iqbal
  • 2,774
  • 1
  • 25
  • 46

1 Answers1

1

Seems the answer is here: https://stackoverflow.com/a/59235979/2118862

Due to diasbling an internal animations, you'll need to do a manual scroll. But keep in mind - while delete an item, do a scroll first, wait until it ends (onAnimationEnd), then reload a model under ViewPager2

A. Petrov
  • 910
  • 13
  • 18