当前位置:主页 > 移动开发 > Android代码 >

ViewPager判断是向左划还是右划的实例

时间:2021-01-02 15:12:10 | 栏目:Android代码 | 点击:

实例如下:

//记录上一次滑动的positionOffsetPixels值
  private int lastValue = -1;
   private boolean isLeft = true;


 mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
      if (positionOffset != 0) {
        if (lastValue >= positionOffsetPixels) {
          //右滑
          isLeft = false;
        } else if (lastValue < positionOffsetPixels) {
          //左滑
          isLeft = true;
        }
        setIndiactorView(positionOffset,isLeft);
      }
      lastValue = positionOffsetPixels;

    }

    @Override
    public void onPageSelected(int position) {
      if (isLeft){
        Log.e("onPageScrolled","--->左划");
      }else {
        Log.e("onPageScrolled","--->右划");

      }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
  });

您可能感兴趣的文章:

相关文章