Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ViewPager manually call PageTransformer transformPage

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?

like image 850
user2635088 Avatar asked Jun 16 '15 12:06

user2635088


1 Answers

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
}
like image 195
Owlvark Avatar answered Oct 13 '22 09:10

Owlvark