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