SHARE
TWEET

Untitled

a guest Jun 18th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Field mFlingDistance;
  2.         mFlingDistance = ViewPager.class.getDeclaredField("mFlingDistance");
  3.         mFlingDistance.setAccessible(true);
  4.  
  5.         // Set custom value:
  6.         mFlingDistance.set(this, 20);
  7.      
  8. package de.XXX.app.Activitys.fragments;
  9.  
  10. import android.content.Context;
  11. import android.support.v4.view.ViewPager;
  12. import android.util.AttributeSet;
  13. import android.view.MotionEvent;
  14. import android.view.View;
  15.  
  16. import java.lang.reflect.Field;
  17. import java.lang.reflect.Modifier;
  18.  
  19. public class VerticalViewPager extends ViewPager {
  20.     public VerticalViewPager(Context context) {
  21.         super(context);
  22.         init();
  23.     }
  24.  
  25.     public VerticalViewPager(Context context, AttributeSet attrs) {
  26.         super(context, attrs);
  27.         init();
  28.     }
  29.  
  30.     private void init() {
  31.  
  32.         try {
  33.             Field mFlingDistance;
  34.             mFlingDistance = ViewPager.class.getDeclaredField("mFlingDistance");
  35.             mFlingDistance.setAccessible(true);
  36.  
  37.             // Set custom value:
  38.             mFlingDistance.set(this, 20);
  39.         }catch (Exception e){
  40.             System.out.println("SMTH Happend!:(");
  41.         }
  42.  
  43.         setPageTransformer(true, new VerticalPageTransformer());
  44.         setOverScrollMode(OVER_SCROLL_NEVER);
  45.     }
  46.  
  47.     private class VerticalPageTransformer implements ViewPager.PageTransformer {
  48.  
  49.         @Override
  50.         public void transformPage(View view, float position) {
  51.  
  52.             if (position < -1) {
  53.                 view.setAlpha(0);
  54.             } else if ( position <= 1) {
  55.                 view.setAlpha(1);
  56.  
  57.                 view.setTranslationX(view.getWidth() * -position);
  58.  
  59.                 float yPosition = position * (view.getHeight()/(1/0.6f));
  60.                 view.setTranslationY(yPosition);
  61.             } else {
  62.                 view.setAlpha(0);
  63.             }
  64.         }
  65.     }
  66.  
  67.     private MotionEvent swapXY(MotionEvent ev) {
  68.         float width = getWidth();
  69.         float height = getHeight();
  70.  
  71.         float newX = (ev.getY() / height) * width;
  72.         float newY = (ev.getX() / width) * height;
  73.  
  74.         ev.setLocation(newX, newY);
  75.  
  76.         return ev;
  77.     }
  78.  
  79.     @Override
  80.     public boolean onInterceptTouchEvent(MotionEvent ev) {
  81.         boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
  82.         swapXY(ev);
  83.         return intercepted;
  84.     }
  85.  
  86.     @Override
  87.     public boolean onTouchEvent(MotionEvent ev) {
  88.         return super.onTouchEvent(swapXY(ev));
  89.     }
  90.  
  91.  
  92.     private static void setFinalStaticField(Class<?> clazz, String fieldName, Object value)
  93.             throws ReflectiveOperationException {
  94.         Field field = clazz.getDeclaredField(fieldName);
  95.         field.setAccessible(true);
  96.         Field modifiers = Field.class.getDeclaredField("modifiers");
  97.         modifiers.setAccessible(true);
  98.         modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
  99.         field.set(null, value);
  100.     }
  101.  
  102. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top