1、原理:主要是通过判断OnPageChangeListener中方法onPageScrollStateChanged的状态来实现
2、主要代码:
viewpager.setOnPageChangeListener(new OnPageChangeListener()
{
boolean isScrolled = false;
@Override
public void onPageScrollStateChanged(int status)
{
switch (status)
{
case 1:// 手势滑动
isScrolled = false;
break;
case 2:// 界面切换
isScrolled = true;
break;
case 0:// 滑动结束
// 当前为最后一张,此时从右向左滑,则切换到第一张
if (viewpager.getCurrentItem() == viewpager.getAdapter()
.getCount() - 1 && !isScrolled)
{
viewpager.setCurrentItem(0);
}
// 当前为第一张,此时从左向右滑,则切换到最后一张
else if (viewpager.getCurrentItem() == 0 && !isScrolled)
{
viewpager.setCurrentItem(viewpager.getAdapter()
.getCount() - 1);
}
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
@Override
public void onPageSelected(int index)
{
index_textview.setText("the " + (index + 1) + " page");
}
});
3、源码地址(含定时滑动):http://download.csdn.net/detail/dianqiugg/6859861