viewpager切换月份天数

viewPager初始化后设置OnPageChangeListener监听
但是手指滑动离开屏幕后,onPageScrollStateChanged的回调经历了1、2,最终没有回调0,这种是什么情况?


```java

06-01 22:35:01.148 16959 16959 D DateView : DateView  
06-01 22:35:01.160 16959 16959 D DateView : onFinishInflate  
06-01 22:35:01.160 16959 16959 D DateView : initView
06-01 22:35:01.163 16959 16959 D DateView : setDate: 2023骞?6鏈?
06-01 22:35:01.166 16959 16959 D DateView : setDate: getDifMonth = 0
06-01 22:35:01.181 16959 16959 D CustomDateView: init: oneHeight = 116.0
06-01 22:35:01.181 16959 16959 D CustomDateView : position: 250
06-01 22:35:01.181 16959 16959 D CustomDateView : setDate: 2023   5
06-01 22:35:01.199 16959 16959 D CustomDateView: init: oneHeight = 116.0
06-01 22:35:01.199 16959 16959 D CustomDateView : position: 249
06-01 22:35:01.199 16959 16959 D CustomDateView : setDate: 2023   4
06-01 22:35:01.213 16959 16959 D CustomDateView: init: oneHeight = 116.0
06-01 22:35:01.213 16959 16959 D CustomDateView : position: 251
06-01 22:35:01.213 16959 16959 D CustomDateView : setDate: 2023   6
06-01 22:35:01.231 16959 16959 D  DateView : onPageScrolled position = 250 , offset = 0.0 , offsetPixels = 0
06-01 22:35:01.233 16959 16959 D  CustomDateView : onDraw
06-01 22:35:01.233 16959 16959 D  CustomDateView : onDraw
06-01 22:35:01.233 16959 16959 D  CustomDateView : onDraw
06-01 22:35:02.876 16959 16959 D  DateView : onPageScrollStateChanged i = 1
06-01 22:35:02.877 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.9982143 , offsetPixels = 1118
06-01 22:35:02.896 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.9875 , offsetPixels = 1106
06-01 22:35:02.915 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.9776786 , offsetPixels = 1095
06-01 22:35:02.933 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.9660714 , offsetPixels = 1082
06-01 22:35:02.950 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.95535713 , offsetPixels = 1070
06-01 22:35:02.967 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.94285715 , offsetPixels = 1056
06-01 22:35:02.987 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.9285714 , offsetPixels = 1040
06-01 22:35:03.002 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.91339284 , offsetPixels = 1023
06-01 22:35:03.020 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.89464283 , offsetPixels = 1002
06-01 22:35:03.038 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.875 , offsetPixels = 980
06-01 22:35:03.055 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.85535717 , offsetPixels = 958
06-01 22:35:03.072 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.8267857 , offsetPixels = 925
06-01 22:35:03.090 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.79375 , offsetPixels = 889
06-01 22:35:03.108 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.75 , offsetPixels = 840
06-01 22:35:03.125 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.69375 , offsetPixels = 777
06-01 22:35:03.143 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.62678576 , offsetPixels = 702
06-01 22:35:03.160 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.5419643 , offsetPixels = 607
06-01 22:35:03.178 16959 16959 D  DateView : onPageScrolled position = 249 , offset = 0.36071426 , offsetPixels = 403
06-01 22:35:03.178 16959 16959 D  DateView : onPageScrollStateChanged i = 2
06-01 22:35:03.178 16959 16959 D  DateView : onPageSelected position = 249 , lastPosition = 250.0 , getCurrentItem() = 249


```

当用户开始滑动ViewPager时,会触发onPageScrollStateChanged()回调,此时状态为1(表示正在拖动),然后onPageScrolled()方法会被调用多次,其中position参数表示当前页面的索引,offset参数表示当前页面的偏移量,offsetPixels参数表示当前页面的像素偏移量。在用户松开手指时,ViewPager会尝试滚动到最近的页面,并触发一次onPageScrolled()回调,此时position参数表示即将显示的页面的索引,offset和offsetPixels参数为0。

  • 你可以看下这个问题的回答https://ask.csdn.net/questions/239455
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:ViewPager的onPageScrolled回调不完整
  • 除此之外, 这篇博客: Android ViewPager控件中的 2. OnPageChangeListener监听器 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • OnPageChangeListener有三个方法

    • onPageScrolled(int, float, int)方法,显示滑动时页面状态。

      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
      

      手势从左往右滑动,position为前一页面,positionOffset从1到0。
      手势从右往左滑动,position为当前页面,positionOffset从0到1。

    • onPageSelected(int)方法,position是被选择页面

      public void onPageSelected(int position)
      
    • onPageScrollStateChanged(int)方法,

      public void onPageScrollStateChanged(int state)
      

      state有三种状态

      • SCROLL_STATE_IDLE,静止状态。
      • SCROLL_STATE_DRAGGING,开始拖曳。
      • SCROLL_STATE_SETTLING,停止拖曳。