12

I have a ViewPager with the PageTransformer implementation that does some funky stuff to the next page of the ViewPager. My implementation looks like this:

class ZoomOutPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.85f;
private static final float MIN_ALPHA = 0.5f;

@Override
public void transformPage(View view, float position) {
    int pageWidth = view.getWidth();

    if (position < -1) { // [-Infinity,-1)
        // This page is way off-screen to the left.
        view.setAlpha(0);

    } else if (position <= 0) { // [-1,0]
        // Use the default slide transition when moving to the left page


    } else if (position < 1) { // (0,1]
      do some funky stuff to the page currently being scrolled into the main view on scrolling left


    } else if (position==1) {
       do some funky stuff to the next page
    }

    else { // (1,+Infinity]
        // This page is way off-screen to the right.
        view.setAlpha(1);
    }
}

}

The problem is that these transform animations are available as soon as the pager is loaded, but they're not available to the first fragment of my viewpager. I.e. the first time a fragment is loaded, the transformPage method is not being called. Is there a way to call the transformPage method manually? I.e. when I'm setting up my ViewPager inside onCreateView? Or somewhere else?

3
  • 1
    I believe that is what it is supposed to do: ViewPager.PageTransformer only animates transitions between pages. That aside, I don't know how you could do what you want without manually animating (without a PageTransformer at all). Jun 16, 2015 at 12:44
  • Hmm ok. Right now, I'm doing a little hack by putting pager.setCurrentItem(1); pager.setCurrentItem(0,true); to trigger the transformPage method. If you have a better way, I'd love to hear it! Jun 16, 2015 at 13:20
  • I am not sure but can you apply the effect to the fragment as a whole, like if you want a fade animation, you can apply that to the viewpager first when it loads up.
    – Sami
    Mar 22, 2016 at 11:47

2 Answers 2

8

I had a similar situation and I ended up using a fake drag to cause the initial transform:

public void onResume() {
    ...
    Handler handler = new Handler();
    final Runnable r = new Runnable() {
        public void run() {
            // force transform with a 1 pixel nudge
            mViewPager.beginFakeDrag();
            mViewPager.fakeDragBy(1.0f);
            mViewPager.endFakeDrag();
        }
    };
    handler.postDelayed(r, 10);  // some small delay in ms
}
2
  • @Appyx it can be simply fixed by checking adapter.getCount() > 0 Dec 16, 2018 at 4:47
  • I do this because of setCurrentItem without smooth scroll, causing the page transformer to not be called.
    – FonzTech
    May 22, 2020 at 21:15
-1

As per the Android Developer's site:

A PageTransformer is invoked whenever a visible/attached page is scrolled. This offers an opportunity for the application to apply a custom transformation to the page views using animation properties So, you wouldn't see any transition animation when the pager is loaded for the first time.

That's a good hack that you have been using, but I don't know if you had tried it already but can you animate the viewpager as a whole itself when it loads for the first time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.