How to disable ViewPager from swiping in one direction

后端 未结 4 2065
清酒与你
清酒与你 2020-12-02 20:36

I want to allow the user swipe in a ViewPager only from right to left. So once he passed a page he can\'t come back to it. How can this be done?

I tried

4条回答
  •  -上瘾入骨i
    2020-12-02 20:55

    package com.contacts_app.jamison.contacts__proprivacy4;
    
    import android.content.Context;
    import android.content.res.Resources;
    import android.support.v4.view.ViewPager;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.MotionEvent;
    
    public class ViewPager_Settings extends ViewPager
    {
    private final String TAG = ViewPager_Settings.class.getSimpleName();
    public float startX;
    
    public ViewPager_Settings(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
     ////////////////////////////////////////////////////////////////////////////////////////////////
    public static int dpTOpx(double dp)
    {
        return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
    }
    public static int pxTOdp(double px)
    {
        return (int) (px / Resources.getSystem().getDisplayMetrics().density);
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    
    /*****DispatchTouchEvent for the View Pager to intercept and block swipes Right*****/
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev)
    {
        final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;
        //int movement_limit = pxTOdp(50);
        switch (actionMasked)
        {
            case (MotionEvent.ACTION_DOWN):
            {
                startX = ev.getX();
                Log.i(TAG, "startX: " + startX);
    
                /*Should always be this below*/
                return super.dispatchTouchEvent(ev);
            }
            case (MotionEvent.ACTION_MOVE):
            {
                Log.i(TAG, "ev.getX() - startX:" + (ev.getX() - startX));
    
                /*Switching directional changes would be a matter of flipping the  "<" sign in the line below.*/
                if (ev.getX() - startX > 0)
                {
                    /*The result is that the ViewPager will not swipe from left*/
                    ev.setAction(MotionEvent.ACTION_CANCEL);;
                }
    
                /*Should always be this below*/
                super.dispatchTouchEvent(ev);
            }
            /**The ACTION_UP case statement is only needed if you don't want to pass down the touch event 
            * to buttons that may receive the click after the swipe is blocked.*/
            /*case (MotionEvent.ACTION_UP):
            {
                //Log.i(TAG, "movement_limit: " + movement_limit);
    
                //(-50) may need to be changed to something more broader in scope to accompany all screen densities
                if ( (ev.getX() - startX) < (-50) )
                {
                    ev.setAction(MotionEvent.ACTION_CANCEL);
                }
    
                //Should always be this below
                super.dispatchTouchEvent(ev);
            }*/
        }
        /*Should always be this below*/
        return super.dispatchTouchEvent(ev);
    }
     ////////////////////////////////////////////////////////////////////////////////////////////////
    
    }/*****END OF FILE*****/
    

    Don't forget to change the line at the top to put the package name of your App. Also most, if not all, of the comments give insight into what the code is doing in case you decide you want to tinker with things.

提交回复
热议问题