Is it possible to somehow limit the possibility of scrolling to one of the parties? It is necessary to give the user the opportunity to scroll only to the right, for example.

    3 answers 3

    You need to override a couple of methods in the ViewPager :

    First, create an enum that will determine in which direction the ViewPager can work:

     public enum SwipeDirection { all, left, right, none ; } 

    Now to the ViewPager itself:

     public class CustomViewPager extends ViewPager { private float initialXValue; private SwipeDirection direction; public CustomViewPager(Context context, AttributeSet attrs) { super(context, attrs); this.direction = SwipeDirection.all; //поумолчанию разрешить листание в любом направлении } @Override public boolean onTouchEvent(MotionEvent event) { if (this.IsSwipeAllowed(event)) { return super.onTouchEvent(event); } return false; } @Override public boolean onInterceptTouchEvent(MotionEvent event) { if (this.IsSwipeAllowed(event)) { return super.onInterceptTouchEvent(event); } return false; } private boolean IsSwipeAllowed(MotionEvent event) { if(this.direction == SwipeDirection.all) //Усли разрешено все return true; if(direction == SwipeDirection.none ) //если листание запрещено return false; //запоминаем начальную позицию тач ивента if(event.getAction()==MotionEvent.ACTION_DOWN) { initialXValue = event.getX(); return true; } //рассчитываем направление свайпа, и запрещаем\разрешаем его if(event.getAction()==MotionEvent.ACTION_MOVE) { try { float diffX = event.getX() - initialXValue; if (diffX > 0 && direction == SwipeDirection.right ) { // свайп влево запрещен return false; }else if (diffX < 0 && direction == SwipeDirection.left ) { // свайп вправо запрещен return false; } } catch (Exception exception) { exception.printStackTrace(); } } return true; } public void setAllowedSwipeDirection(SwipeDirection direction) { this.direction = direction; } } 

    Now add it to the markup instead of the usual ViewPager

     <тут_ваш_пакедж.CustomViewPager android:id="@+id/customViewPager" android:layout_height="match_parent" android:layout_width="match_parent" /> 

    And now in the code we set the permission / ban on svayp:

     mViewPager.setAllowedSwipeDirection(SwipeDirection.right); 

    So it will just right.

      hang OnPageChangeListener to it, and make sure that the new value is not more (or less) than the current one

        You can try to override the performDrag method of the performDrag class:

         private boolean performDrag(float x) { boolean needsInvalidate = false; final float deltaX = mLastMotionX - x; mLastMotionX = x; // начало нового кода if (deltaX < 0) { return false; } // окончание нового кода float oldScrollX = getScrollX(); ... }