How to know viewpager is scroll left or right?

前端 未结 13 1816
梦谈多话
梦谈多话 2020-12-13 06:18

I am using ViewPager (support library). I want to know every time the ViewPager change the visible page, it is scrolling left or right.

Please give me a solution. An

相关标签:
13条回答
  • 2020-12-13 06:57

    It's not a perfect solution but here's a way to check the swipe direction when you start swiping:

    new ViewPager.OnPageChangeListener() {
    
                private static final float thresholdOffset = 0.5f;
                private boolean scrollStarted, checkDirection;
    
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    if (checkDirection) {
                        if (thresholdOffset > positionOffset) {
                            Log.i(C.TAG, "going left");
                        } else {
                            Log.i(C.TAG, "going right");
                        }
                        checkDirection = false;
                    }
                }
    
                @Override
                public void onPageSelected(int position) {}
    
                @Override
                public void onPageScrollStateChanged(int state) {
                    if (!scrollStarted && state == ViewPager.SCROLL_STATE_DRAGGING) {
                        scrollStarted = true;
                        checkDirection = true;
                    } else {
                        scrollStarted = false;
                    }
                }
            });
    


    EDIT: there's a more elegant approach that involves using a ViewPager.PageTransformer and checking it's position intervals:

    ...
    myViewPager.setPageTransformer(true, new PageTransformer());
    ...
    
    public class PageTransformer implements ViewPager.PageTransformer {
        public void transformPage(View view, float position) {
    
            if (position < -1) { 
                // [-00,-1): the page is way off-screen to the left.
            } else if (position <= 1) { 
                // [-1,1]: the page is "centered"
            } else { 
                // (1,+00]: the page is way off-screen to the right.           
            }
        }
    }
    

    You can learn more from: Using ViewPager for Screen Slides

    0 讨论(0)
  • 2020-12-13 06:58

    use that

    @Override
    public void onPageSelected( int position )
    {
        mCurrentFragmentPosition = position;
    }
    @Override
    public void onPageScrolled( int position, float positionOffset, int positionOffsetPixels )
    {
        boolean isGoingToRightPage = position == mCurrentFragmentPosition;
        if(isGoingToRightPage)
        {
            // user is going to the right page
        }
        else
        {
            // user is going to the left page
        }
    }
    
    0 讨论(0)
  • 2020-12-13 07:00

    set setOnPageChangeListener to your ViewPager

    keep a variable global as

    private int lastPosition = 0;
    

    and in

    @Override
    public void onPageSelected(int arg0) {
        if (lastPosition > position) {
             System.out.println("Left");
          }else if (lastPosition < position) {
             System.out.println("Right");
          }
          lastPosition = position;
    }
    
    0 讨论(0)
  • 2020-12-13 07:09

    You can keep class member variable to save last visited page

    private int mLastVisitedPageIndex = 0;
    

    Then use following function to check direction

    @Override
    public void onPageSelected(int i) {
        boolean isMovingForward = mLastVisitedPageIndex < i?true:false;
        //Use isMovingForward variable anywhere now
        mLastVisitedPageIndex = i;
    }
    
    0 讨论(0)
  • 2020-12-13 07:12
    private float sumPositionAndPositionOffset;
    
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        boolean isSwipeToLeft = position + positionOffset > sumPositionAndPositionOffset;
        sumPositionAndPositionOffset = position + positionOffset;   
    }
    
    0 讨论(0)
  • 2020-12-13 07:12

    Apologies - had to edit the answer as I found a bug. Here is improved solution:

    The solution compares current page index with one previously selected (previousPageIndex)

    newPageIndex represents the page which is about to be scrolled to.

    Condition (positionOffset == 0) compares if the scroll finished

    private int previousPageIndex = 0;
    private int newPageIndex = -1;
    
    private final int MOVE_DIRECTION_NONE = 0;
    private final int MOVE_DIRECTION_LEFT = 1;
    private final int MOVE_DIRECTION_RIGHT = 2;
    
    private int moveDirection = MOVE_DIRECTION_NONE;
    
    
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        super.onPageScrolled(position, positionOffset, positionOffsetPixels);
    
        if (moveDirection == MOVE_DIRECTION_NONE) {
            if (previousPageIndex  == position){
                moveDirection = MOVE_DIRECTION_LEFT;
                if (newPageIndex == -1) newPageIndex = previousPageIndex + 1;
            }  else {
                moveDirection = MOVE_DIRECTION_RIGHT;
                if (newPageIndex == -1) newPageIndex = previousPageIndex - 1;
            }
        }
    
        if (positionOffset == 0) {
            System.out.println("Reseting");
            previousPageIndex = position;
            moveDirection = MOVE_DIRECTION_NONE;
            newPageIndex = -1;
        }
    
        switch (moveDirection) {
            case MOVE_DIRECTION_LEFT:
                if (onPageChangingHandler != null) onPageChangingHandler.pageChanging(previousPageIndex, newPageIndex, positionOffset);
                System.out.println("Sliding Left | Previous index: " + previousPageIndex + " | New Index: " + newPageIndex + " | offset: " + positionOffset  + " | Position: " + position);
                break;
            case MOVE_DIRECTION_RIGHT:
                if (onPageChangingHandler != null) onPageChangingHandler.pageChanging(newPageIndex, previousPageIndex, positionOffset);
                System.out.println("Sliding Right | Previous index: " + previousPageIndex + " | New Index: " + newPageIndex + " | offset: " + positionOffset + " | Position: " + position);
                break;
            case MOVE_DIRECTION_NONE:
                System.out.println("Moving NONE");
                break;
        }
    }
    
    0 讨论(0)
提交回复
热议问题