1 package com.davemorrissey.labs.subscaleview.test.viewpager; 2 3 import android.content.Context; 4 import android.support.annotation.NonNull; 5 import android.support.v4.view.ViewPager; 6 import android.util.AttributeSet; 7 import android.view.MotionEvent; 8 import android.view.View; 9 10 /** 11 * From http://stackoverflow.com/a/22797619/2719186 12 */ 13 public class VerticalViewPager extends ViewPager { 14 VerticalViewPager(Context context)15 public VerticalViewPager(Context context) { 16 super(context); 17 init(); 18 } 19 VerticalViewPager(Context context, AttributeSet attrs)20 public VerticalViewPager(Context context, AttributeSet attrs) { 21 super(context, attrs); 22 init(); 23 } 24 init()25 private void init() { 26 setPageTransformer(true, new VerticalPageTransformer()); 27 setOverScrollMode(OVER_SCROLL_NEVER); 28 } 29 30 private class VerticalPageTransformer implements ViewPager.PageTransformer { 31 32 @Override transformPage(@onNull View view, float position)33 public void transformPage(@NonNull View view, float position) { 34 if (position < -1) { 35 view.setAlpha(0); 36 } else if (position <= 1) { 37 view.setAlpha(1); 38 view.setTranslationX(view.getWidth() * -position); 39 float yPosition = position * view.getHeight(); 40 view.setTranslationY(yPosition); 41 } else { 42 view.setAlpha(0); 43 } 44 } 45 } 46 swapXY(MotionEvent ev)47 private MotionEvent swapXY(MotionEvent ev) { 48 float width = getWidth(); 49 float height = getHeight(); 50 float newX = (ev.getY() / height) * width; 51 float newY = (ev.getX() / width) * height; 52 ev.setLocation(newX, newY); 53 return ev; 54 } 55 56 @Override onInterceptTouchEvent(MotionEvent ev)57 public boolean onInterceptTouchEvent(MotionEvent ev){ 58 boolean intercepted = super.onInterceptTouchEvent(swapXY(ev)); 59 swapXY(ev); 60 return intercepted; 61 } 62 63 @Override onTouchEvent(MotionEvent ev)64 public boolean onTouchEvent(MotionEvent ev) { 65 return super.onTouchEvent(swapXY(ev)); 66 } 67 68 } 69