android ViewPager之OnPageChangeListener接口


 项目中在使用ViewPager的时候,一般都要在界面滑动的时候做一些事情,android中有个专门的状态回调接口OnPageChangeListener。

/**
* Callback interface for responding to changing state of the selected page.
*/
public interface OnPageChangeListener {

/**
* This method will be invoked when the current page is scrolled, either as part
* of a programmatically initiated smooth scroll or a user initiated touch scroll.
*
* @param position Position index of the first page currently being displayed.
* Page position+1 will be visible if positionOffset is nonzero.
* @param positionOffset Value from [0, 1) indicating the offset from the page at position.
* @param positionOffsetPixels Value in pixels indicating the offset from position.
*/
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels);

/**
* This method will be invoked when a new page becomes selected. Animation is not
* necessarily complete.
*
* @param position Position index of the new selected page.
*/
public void onPageSelected(int position);

/**
* 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.
*
* @param state The new scroll state.
* @see ViewPager#SCROLL_STATE_IDLE
* @see ViewPager#SCROLL_STATE_DRAGGING
* @see ViewPager#SCROLL_STATE_SETTLING
*/
public void onPageScrollStateChanged(int state);
}

 
SDK中关于ViewPager OnPageChangeListener 的介绍如上.
public  void   onPageScrolled  (  int   position ,  float   positionOffset  ,   int   positionOffsetPixels )  ;
position:当前页码
positionOffset 页面偏移量(百分比) 当往右滑动时递增,往左滑动时递减。
positionOffsetPixels 页面像素偏移量 当往右滑动时递增,往左滑动时递减。 
 
 
public void onPageSelected(int position)
position:当前选中的页码 
 
 
public void onPageScrollStateChanged(int state)
state:滑动时,页面的状态。在ViewPager.class中定义了三个状态,分布为:
 


/**
* Indicates that the pager is in an idle, settled state. The current page
* is fully in view and no animation is in progress.
*/
public static final int SCROLL_STATE_IDLE = 0;

/**
* Indicates that the pager is currently being dragged by the user.
*/
public static final int SCROLL_STATE_DRAGGING = 1;

/**
* Indicates that the pager is in the process of settling to a final position.
*/
public static final int SCROLL_STATE_SETTLING = 2;

 
SCROLL_STATE_IDLE : 值为0,表示当前页已经选定。
SCROLL_STATE_DRAGGING: 值为1,表示当前页面正在拖动。
SCROLL_STATE_SETTING: 值为2,表示当前页面正在拖动中,还没有到选定状态。
 
在项目使用中,可以在onPageScrolled中得到页面的偏移量来设置滑动距离。
在onPageStateChanged中,通过页面的状态来进行处理相关的逻辑。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM