今天在实现轮播的时候需要监听ViewPager的滑动时间,所以就研究了一下这个监听事件,记录一下。
首先这个监听事件添加之后需要我们来实现三个回调方法,分别是:onPageSelected(int position)、onPageScrollStateChanged(int state)、onPageScrolled(int position,float positionOffset,int positionOffsetPixels)。
在测试的时候,我直接实现了这个接口,然后根据输出的日志来分析结果,整个实现接口的类如下:
1.onPageScrollStateChanged (int state)。
首先看一下官方的解释:
public abstract void onPageScrollStateChanged (int state)
Called when the scroll state changes. Useful for discovering when the user begins dragging, when the pager is automatically settling to the current page, or when it is fully stopped/idle.
Parameters
state The new scroll state.
See Also
SCROLL_STATE_IDLE
SCROLL_STATE_DRAGGING
SCROLL_STATE_SETTLING
也就是说当滚动状态发生改变的时候会回调这个方法,同时传递进来一个此刻滚动状态的参数。
参数的值为:
public static final int SCROLL_STATE_DRAGGING
Indicates that the pager is currently being dragged by the user.
Constant Value: 1 (0x00000001)
public static final int SCROLL_STATE_IDLEIndicates that the pager is in an idle, settled state. The current page is fully in view and no animation is in