Viewpager在OnePlus 5T设备中无法正确滑动

时间:2019-02-24 12:11:14

标签: android android-viewpager android-pagetransformer

我在android应用中,我需要一个可垂直滑动的viewpager(向上     向下的方式)。为此,我制作了一个自定义viewpager     使用传统的viewpager和应用的PageTransfirmer进行制作     垂直而不是水平滑动。其他一切都正常     除One Plus 5t(Andriod版本9)以外的其他设备

My code is below:

    public class VerticalViewPager extends ViewPager {

    private float initialXValue;
    private float initialYValue;
    private float minXDifference = 200;
    private float minYDifference = 100;
    public static SwapListener swapListener;
    public static String SwipeLeft = "left";
    public static String SwipeRight = "right";
    public static boolean swipeTriggered = false;
    public static boolean verticalSwipeTriggered = false;
    private FixedSpeedScroller mScroller = null;
    private boolean enabled;

    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);

    try {
    Class<?> viewpager = ViewPager.class;
    Field scroller = viewpager.getDeclaredField("mScroller");
    scroller.setAccessible(true);
    mScroller = new FixedSpeedScroller(getContext(),
    new DecelerateInterpolator());
    scroller.set(this, mScroller);
    } catch (Exception ignored) {
    }
    }

    /*
     * Set the factor by which the duration will change
     */
    public void setScrollDuration(int duration) {
    mScroller.setScrollDuration(duration);
    }

    private class FixedSpeedScroller extends Scroller {

    private int mDuration = 1000;

    public FixedSpeedScroller(Context context) {super(context);}

    public FixedSpeedScroller(Context context, Interpolator     interpolator) {
    super(context, interpolator);
    }

    public FixedSpeedScroller(Context context, Interpolator     interpolator, 
    boolean flywheel) {super(context, interpolator, flywheel);}

    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int 
    duration) {
    // Ignore received duration, use fixed one instead
    super.startScroll(startX, startY, dx, dy, mDuration);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy) {
    // Ignore received duration, use fixed one instead
    super.startScroll(startX, startY, dx, dy, mDuration);
    }

    public void setScrollDuration(int duration) {mDuration = duration;}
    }

    private class VerticalPageTransformer implements 
    ViewPager.PageTransformer {

    @Override
    public void transformPage(View view, float position) {

    if (position < -1) { // [-Infinity,-1)
    // This page is way off-screen to the left.
    view.setAlpha(0);
    }
    else if (position <= 1) {
    view.setAlpha(1);

    // Counteract the default slide transition
    view.setTranslationX(view.getWidth() * -position);
    //set Y position to swipe in from top
    float yPosition = position * view.getHeight();
    view.setTranslationY(yPosition);
    }
   else {
   // This page is way off-screen to the right.
   view.setAlpha(0);
   }
   }
   }

   /**
    * Swaps the X and Y coordinates of your touch event.
    */
   private MotionEvent swapXY(MotionEvent ev) {
   float width = getWidth();
   float height = getHeight();
   float newX = (ev.getY() / height) * width;
   float newY = (ev.getX() / width) * height;
   ev.setLocation(newX, newY);
   return ev;
    }

   @Override
   public boolean onInterceptTouchEvent(MotionEvent ev){
   boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
   swapXY(ev); // return touch coordinates to original reference frame         
   for any child views
   //IsSwipeAllowed(ev);
   return intercepted;
   }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
    IsSwipeAllowed(ev);
    return super.onTouchEvent(swapXY(ev));
    }


    private void IsSwipeAllowed(MotionEvent event) {

    if(event.getAction() == MotionEvent.ACTION_DOWN) {
    initialXValue = event.getX();
    initialYValue = event.getY();
    }

    if(event.getAction()==MotionEvent.ACTION_MOVE) {
    try {
    float diffX = Math.abs(event.getX() - initialXValue);
    float diffy = Math.abs(event.getY() - initialYValue);
    if (diffX > diffy && diffX > minXDifference) {
    // swipe horizotal
    if (!swipeTriggered && event.getX() > initialXValue) {
    swapListener.listenSwapEvent(SwipeRight);           
    swipeTriggered = true;
    }
    else if (event.getX() < initialXValue) {
    if (!HomeScreen.projectName.equals("ABMCPL") && 
    CustomViewPager.IsSwipeAllowed(event) && !swipeTriggered) {
    swapListener.listenSwapEvent(SwipeLeft);        // to webview page
    swipeTriggered = true;
    }
    }
    }
    else if (diffX < diffy && diffy > minYDifference) {
    if (!verticalSwipeTriggered && event.getY() > initialYValue) { 
    viewPager.setCurrentItem(LandingPage.viewPager.getCurrentItem() -     1);
    verticalSwipeTriggered = true;
    }
    else if (!verticalSwipeTriggered && event.getY() < initialYValue){

    verticalSwipeTriggered = true;
    }
    }
    } catch (Exception exception) {
    exception.printStackTrace();
    }
    }
    }

    public interface SwapListener {
    void listenSwapEvent (String direction);
    }
    }

0 个答案:

没有答案