android ViewPager之OnPageChangeListener接口

本文详细介绍了Android中ViewPager的滑动监听接口OnPageChangeListener。包括其三个主要方法:onPageScrolled、onPageSelected和onPageScrollStateChanged的具体作用及参数含义,并提供了不同状态下可以进行的操作建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 项目中在使用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值