viewPager滑动到最后一张怎么进入下个页面
展开全部
public class LastPageJumpListener implements OnPageChangeListener {
private int lastIndex;
private Runnable command;
/**
* 是否可以跳转
*/
private boolean canJump;
private int curPosition;
/**
* @param lastIndex
* 最后一页的索引
* @param command
* 事件触发时你想要做的事情
*/
public LastPageJumpListener(int lastIndex, Runnable command) {
this.lastIndex = lastIndex;
this.command = command;
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
Log.e(TAG, "onPageScrolled position:" + position);
//当最后一页往后划动的时候触发该事件
if (position == lastIndex && positionOffset == 0
&& positionOffsetPixels == 0) {
if (canJump) {
command.run();
// 事件执行一次后进行重置,避免事件多次触发
canJump = false;
}
}
}
/*
* (non-Javadoc)
*
* @see
* android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected
* (int)
*/
@Override
public void onPageSelected(int position) {
curPosition = position;
}
/**
* public static final int SCROLL_STATE_IDLE = 0;
*
* public static final int SCROLL_STATE_DRAGGING = 1;
*
* public static final int SCROLL_STATE_SETTLING = 2;
*/
@Override
public void onPageScrollStateChanged(int state) {
Log.e(TAG, "onPageScrollStateChanged state:" + state);
// 判断是否是划动状态且是最后一页
if (state == ViewPager.SCROLL_STATE_DRAGGING
&& curPosition == lastIndex) {
canJump = true;
} else {
canJump = false;
}
}
}
这是我自己写的,实测可以用,不懂你可以问我
private int lastIndex;
private Runnable command;
/**
* 是否可以跳转
*/
private boolean canJump;
private int curPosition;
/**
* @param lastIndex
* 最后一页的索引
* @param command
* 事件触发时你想要做的事情
*/
public LastPageJumpListener(int lastIndex, Runnable command) {
this.lastIndex = lastIndex;
this.command = command;
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
Log.e(TAG, "onPageScrolled position:" + position);
//当最后一页往后划动的时候触发该事件
if (position == lastIndex && positionOffset == 0
&& positionOffsetPixels == 0) {
if (canJump) {
command.run();
// 事件执行一次后进行重置,避免事件多次触发
canJump = false;
}
}
}
/*
* (non-Javadoc)
*
* @see
* android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected
* (int)
*/
@Override
public void onPageSelected(int position) {
curPosition = position;
}
/**
* public static final int SCROLL_STATE_IDLE = 0;
*
* public static final int SCROLL_STATE_DRAGGING = 1;
*
* public static final int SCROLL_STATE_SETTLING = 2;
*/
@Override
public void onPageScrollStateChanged(int state) {
Log.e(TAG, "onPageScrollStateChanged state:" + state);
// 判断是否是划动状态且是最后一页
if (state == ViewPager.SCROLL_STATE_DRAGGING
&& curPosition == lastIndex) {
canJump = true;
} else {
canJump = false;
}
}
}
这是我自己写的,实测可以用,不懂你可以问我
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询