项目中在使用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中,通过页面的状态来进行处理相关的逻辑。

转载于:https://www.cnblogs.com/Theone2014/p/4748569.html

更多相关文章

  1. Android VideoView状态跟踪和错误处理
  2. Android 判断屏幕开关状态方式总结
  3. Android 获取手机网络状态
  4. Android获取屏幕状态的方式
  5. Android实现购物车整体页面逻辑详解
  6. 记录状态栏与布局重合,状态栏颜色问题
  7. Android沉浸式状态栏实现
  8. Android 打开网络设置页面
  9. android 3g状态及信号监测

随机推荐

  1. Android布局属性详解
  2. 【Android车载系统 News | Tech 5】车载
  3. Android™ 1.5 android.R.drawable Icon
  4. Android(安卓)线程归纳
  5. Android中使用HttpURLConnection和HttpCl
  6. view属性大全
  7. Android布局属性全面剖析
  8. Android——RatingBar(评价条)相关知识总结
  9. 2014.02.11 ——— android volley 打包
  10. 使用命令建立简单的布局