Skip to content

Instantly share code, notes, and snippets.

@messenger63
Created March 25, 2015 12:41
Show Gist options
  • Save messenger63/50d931f4ee7fbd035bae to your computer and use it in GitHub Desktop.
Save messenger63/50d931f4ee7fbd035bae to your computer and use it in GitHub Desktop.
vertical view pager
package com.nix.betavest.activities;
/**
* Created by vlevytskyy on 18.03.2015.
*/
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* Uses a combination of a PageTransformer and onTouchEvent to create the
* illusion of a vertically scrolling ViewPager.
*
* Requires API 11+
*
*/
@SuppressLint("NewApi")
public class VerticalViewPager extends ViewPager {
public VerticalViewPager(Context context) {
super(context);
init();
}
public VerticalViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
// The majority of the magic happens here
setPageTransformer(true, new VerticalPageTransformer());
// The easiest way to get rid of the overscroll drawing that happens on the left and right
setOverScrollMode(OVER_SCROLL_NEVER);
}
private class VerticalPageTransformer implements ViewPager.PageTransformer {
@Override
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
int pageHeight = view.getHeight();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 1) { // [-1,1]
view.setAlpha(1);
// Counteract the default slide transition
view.setTranslationX(pageWidth * -position);
//set Y position to swipe in from top
float yPosition = position * pageHeight;
view.setTranslationY(yPosition);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}
/**
* Swaps the X and Y coordinates of your touch event
*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
//swap the x and y coords of the touch event
ev.setLocation(ev.getY(), ev.getX());
return super.onTouchEvent(ev);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment