How do you detect that a page has finished sliding in ViewPager.PageTransformer.transformPage method?

754 Views Asked by At

Additionally, does the transformPage method get called on both the incoming and outgoing views simultaneously? How do I make an animation for outgoing views to the left, and animation for incoming views from the right?

1

There are 1 best solutions below

0
Gaurav On

Yes, transformPage method gets called on both the view simultaneously. You get current view instance in transformPage method's first parameter.

public void transformPage(View view, float position) {...}

Second parameter is position of view. As explained in documentation

Position of page relative to the current front-and-center * position of the pager. 0 is front and center. 1 is one full * page position to the right, and -1 is one page position to the left.

A Simple FadIn and FadOut transformation can be applied with following code

public class MyFadePageTransformer implements ViewPager.PageTransformer {
    public void transformPage(View view, float position) {

        //You can set Tag to view object and getTag to know current view
        //Log.d("Page Transform", "page " + view.getTag().toString());

        if (position < -1 || position > 1) {
            view.setAlpha(0);
        }
        else if (position <= 0 || position <= 1) {
            // Calculate alpha. Position is decimal in [-1,0] or [0,1]
            float alpha = (position <= 0) ? position + 1 : 1 - position;
            view.setAlpha(alpha);
        }
        else if (position == 0) {
            view.setAlpha(1);
        }
    }
}

Then add PageTransformer to ViewPager

mViewPager.setPageTransformer(false, new MyFadePageTransformer());