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

android ViewPager之OnPageChangeListener接口的更多相关文章

  1. Android ViewPager

    昨天看到Weather&Clock Widget的页面滑动效果不错,了解了下可能是使用ViewPager来实现的,今天研究下,顺便记录下来.   根据Android官网的介绍,ViewPage ...

  2. Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡

     <Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡> 之前基于github上的第三方开源控件ViewPagerIndicator的UnderlinePa ...

  3. Android ViewPager再探:增加滑动指示条

    上一篇:<Android ViewPager初探:让页面滑动起来> ViewPager只是左右滑动有些丑,也不知道当前位于第几页面. 可以在上方加入滑动指示条,来确定当前位置. 只需要修改 ...

  4. Android ViewPager 打造炫酷欢迎页

    Android ViewPager 打造炫酷欢迎页 ViewPager是Android扩展v4包中的类,这个类可以让用户切换当前的View.对于这个类的应用场景,稍加修改就可以应用到多个环境下.比如: ...

  5. xamarin android viewpager的用法

    1.什么是ViewPager 通过手势滑动可以完成view的切换,一般是用来app的引导页或则实现图片轮播,类似网页上的banner轮播. Adnroid 3.0后引入的一个UI控件,在xamarin ...

  6. Android ViewPager用法小结

    android-support-v4.jar 是谷歌提供给我们的一个兼容低版本号安卓设备的软件包.里面包囊了仅仅有在 Android 3.0 以上可用的API.而 ViewPager 就是当中之中的一 ...

  7. 解决实现OnPageChangeListener()接口的方法参数出现arg0,arg1的现象

    在安卓开发中,我们经常用到ViewPager,那么既然用到ViewPager,一般都需要去实现OnPageChangeListener()接口的三个方法. 但是有时候实现的三个方法的参数都变成agr0 ...

  8. Android ViewPager使用方法小结

    android-support-v4.jar 是谷歌提供给我们的一个兼容低版本安卓设备的软件包,里面包囊了只有在 Android 3.0 以上可用的API.而 ViewPager 就是其中之一.利用它 ...

  9. Android ViewPager 用法

    Android ViewPager 用法 场景:一般第一次打开应用程序时,程序会有一个提示页来给展现应用程序都有哪些功能:或者程序更新时,又更新哪些新特性,都可以使用ViewPager Demo 描述 ...

随机推荐

  1. ARP(Adress Resolution Protocol): 地址解析协议

    地址解析协议(Address Resolution Protoclol),其基本功能为通过目标设备的IP地址,查询目标设备的MAC地址,以保证通信的顺利.它是IPV4中网络层必不可少的协议.不过在IP ...

  2. Gradle及eclipse插件安装

    1.  下载Gradle Gradle需要jdk7或以上版本,使用Java –version命令进行测试:Gradle自带了Groovy库,所以无需再安装Groovy,已经安装的Groovy会被Gra ...

  3. CF954F Runner's Problem(动态规划,矩阵快速幂)

    CF954F Runner's Problem(动态规划,矩阵快速幂) 题面 CodeForces 翻译: 有一个\(3\times M\)的田野 一开始你在\((1,2)\)位置 如果你在\((i, ...

  4. 解题:AHOI 2013 作业

    题面 emmm......我把莫队扔到了杂题里,因为感觉局限挺大的=.= 这题是莫队维护信息+分块查询答案,都是两者的基本操作,复杂度$O(m$ $sqrt(n)+n$ $sqrt(m))$ 所以为啥 ...

  5. 【noip2018】【luogu5021】赛道修建

    题目描述 C 城将要举办一系列的赛车比赛.在比赛前,需要在城内修建 mm 条赛道. C 城一共有 nn 个路口,这些路口编号为 1,2,…,n1,2,…,n,有 n-1n−1 条适合于修建赛道的双向通 ...

  6. 04-树6. Huffman Codes--优先队列(堆)在哈夫曼树与哈夫曼编码上的应用

    题目来源:http://www.patest.cn/contests/mooc-ds/04-%E6%A0%916 In 1953, David A. Huffman published his pap ...

  7. fork系统炸弹

    最近偶然看到"fork系统炸弹"的代码,小小一行shell竟然能够直接搞死系统,令人印象深刻. 代码如下 :(){ :|:& };: 咋一看有点蒙,重新排版下格式 :() ...

  8. 《提升c++性能的编程技术》读书笔记

    http://note.youdao.com/noteshare?id=9ab0eda264c85b774021426867e18eae

  9. 1.4 Matplotlib:绘图

    sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...

  10. eclipse插件大全(官方)

    eclipse插件大全:http://marketplace.eclipse.org/metrics/successful_installs 各个版本插件: http://download.eclip ...