1

Is there any way to combine multiple transformations into just one custom transformation that can be applied to a view pager?

For example, I have this view pager layout with two image views.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ImageView
        android:id="@+id/image"
        android:layout_width="150dp"
        android:layout_height="150dp"/>

</RelativeLayoutLayout>

I want the first image view to transform according this tranformation

public class FadePageTransformer implements ViewPager.PageTransformer {

    public void transformPage(View view, float position) {



        view.setTranslationX(view.getWidth() * -position);

        if(position <= -1.0F || position >= 1.0F) {
            view.setAlpha(0.0F);
        } else if( position == 0.0F ) {
            view.setAlpha(1.0F);
        } else {
            // position is between -1.0F & 0.0F OR 0.0F & 1.0F
            view.setAlpha(1.0F - Math.abs(position));
        }
    }
}

And I want the second view to transform according some other custom transformation

public class CustomTranform implements ViewPager.PageTransformer {

    public void transformPage(View view, float position) {

        //Some code
    }
}
the_prole
  • 8,275
  • 16
  • 78
  • 163

1 Answers1

1

This method worked great for me. I just synced two pagers together.

viewPager.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        viewPager2.onTouchEvent(event);
        return false;
    }
});

viewPager2.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        viewPager.onTouchEvent(event);
        return false;
    }
});

Credit goes here.

Community
  • 1
  • 1
the_prole
  • 8,275
  • 16
  • 78
  • 163