How to know viewpager is scroll left or right?

前端 未结 13 1818
梦谈多话
梦谈多话 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 07:14
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    
            private int mCurrentSelectedScreen;
            private int mNextSelectedScreen;
            private static final float thresholdOffset = 0.5f;
            private boolean scrollStarted=true, checkDirection=false;
            ArrayList<Integer> comp_ary=new ArrayList<Integer>();
    
            @Override
            public void onPageSelected(int arg0) {
            }
    
            @Override
            public void onPageScrolled(int position, float positionOffset,
                    int positionOffsetPixels) {
    
            //Log.e("positionOffsetPixels : "+positionOffsetPixels, "positionOffset : "+positionOffset);
            comp_ary.add(positionOffsetPixels);
                 if (checkDirection) {
                        if (comp_ary.get(2) < comp_ary.get(comp_ary.size()-1)) {
    
                            Log.e("going left", "going left");
                        } else
                            if (comp_ary.get(2) > comp_ary.get(comp_ary.size()-1))
                        {
    
                            Log.e("going right", "going right");
                        }
                        checkDirection = false;
                        comp_ary=new ArrayList<Integer>();
                    }
            }
    
            @Override
            public void onPageScrollStateChanged(int arg0) {
    
    
    
    
                if (!scrollStarted && arg0 == ViewPager.SCROLL_STATE_SETTLING) {
                    scrollStarted = true;
                    checkDirection = true;
                } else {
                    scrollStarted = false;
                }
    
    
            }
        });
    
    0 讨论(0)
  • 2020-12-13 07:16
    1. This is my simple solution in the onPageScrolled() method of ViewPager.OnPageChangeListener:

    0 讨论(0)
  • 2020-12-13 07:17

    Same solution as GuilhE with a minor fix to avoid getting false positives when paging left (swiping right) on the first page(no more pages to the left) in the ViewPager. It simply does an additional check to see if the swipe has actually moved at all.

    new ViewPager.OnPageChangeListener() {
    
      private static final float thresholdOffset = 0.5f;
      private static final int thresholdOffsetPixels = 1;
      private boolean scrollStarted, checkDirection;
    
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (checkDirection) {
          if (thresholdOffset > positionOffset && positionOffsetPixels > thresholdOffsetPixels) {
            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;
        }
      }
     });
    
    0 讨论(0)
  • 2020-12-13 07:19

    Use the ViewPager.OnPageChangeListener interface. You can use the position argument passed to onPageSelected and compare it to the previous value to figure out which way the ViewPager was scrolled.

    0 讨论(0)
  • 2020-12-13 07:19

    I solved the issue with this implementation. Hope it helps.

    public static final float EPSILON= 0.001f;
    
    @Override
    public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) {
    
        // initial position (positionOffset == 0)
        if (positionOffset < EPSILON) {
            mIsRight = positionOffset < 0.5;
            return;
        }
    
        // code here
        if (mIsRight) {
        } else {
        }
    }
    
    0 讨论(0)
  • 2020-12-13 07:20
    pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
                if(position  == pager.getCurrentItem()){
                    // Move Right
                }
                else{
                    // Move Left
                }
    
            }
    
    0 讨论(0)
提交回复
热议问题