在网上看到了viewpager之后自己看了看,效果不错,同样eoe社区也有很多相关的文章,比如/?mod=viewthread&tid=157771&page=21#pid1384160,大家可以看看,使用viewpager的时候大家不要忘了导入这个包,自己可以去下载。
但是在使用的时候发现以上找到的viewpager不能实现循环滑动,这对于用户体验可能不是太好,所以自己又开始在此基础上寻找其他的方法,最终发现了以下解决办法:
将MyPagerAdapter修改一下:
/**
* ViewPager适配器
*/
public class MyPagerAdapter extends PagerAdapter {
public List<View> views;
Context context;
int mCount;
public MyPagerAdapter(Context context,List<View> views) {
= views;
=context;
mCount = ();
}
@Override
public void destroyItem(View collection, int position, Object arg2) {
if (position >= ()) {
int newPosition = position%();
position = newPosition;
// ((ViewPager) collection).removeView((position));
}
if(position <0){
position = -position;
// ((ViewPager) collection).removeView((position));
}
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public int getCount() {
return mCount+1;//此处+1才能向右连续滚动
}
@Override
public Object instantiateItem(View collection, int position) {
if (position >= ()) {
int newPosition = position%();
position = newPosition;
mCount++;
}
if(position <0){
position = -position;
mCount--;
}
try {
((ViewPager) collection).addView((position), 0);
} catch (Exception e) {
}
return (position);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == (object);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
}
同时如果你要的效果里面有上面链接中的那个白色的动画效果,同样也需要再修改一个地方
/**
* 页卡切换监听,用于改变动画位置
*/
public class MyOnPageChangeListener implements OnPageChangeListener {
int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
int two = one * 2;// 页卡1 -> 页卡3 偏移量
@Override
public void onPageSelected(int arg0) {
Animation animation = null;
if(arg0>2){
arg0=arg0%3;
}
switch (arg0) {
case 0:
if (currIndex == 1) {
animation = new TranslateAnimation(one, 0, 0, 0);
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, 0, 0, 0);
}
break;
case 1:
if (currIndex == 0) {
animation = new TranslateAnimation(offset, one, 0, 0);
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, one, 0, 0);
}
break;
case 2:
if (currIndex == 0) {
animation = new TranslateAnimation(offset, two, 0, 0);
} else if (currIndex == 1) {
animation = new TranslateAnimation(one, two, 0, 0);
}
break;
}
currIndex = arg0;
(true);// True:图片停在动画结束位置
(300);
(animation);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
}
这样一来,其他地方不需要修改,即可实现viewpager的循环滑动效果
自己继续修改了以下,发现可以实现向左无限循环(貌似是无限)的,我的方法如下
有几个方法做以下改动就行了:@Override
public void destroyItem(View collection, int position, Object arg2) {
//循环滑动时此处不能销毁
// ((ViewPager) collection).removeView((position%()));
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public int getCount() {
return Integer.MAX_VALUE;//设置成最大值以便循环滑动
}
@Override
public Object instantiateItem(View collection, int position) {
try {
((ViewPager) collection).addView((position%()), 0);
} catch (Exception e) {
}
return (position%());
}
最后再初始化页面时(3*100);//设置初始页面,为0的话开始不能向左滑动
这样的话就能实现类似的无限循环(向左其实只有100次,只是一般没人会在那儿向左移动那么多次而已)