在网上看到了viewpager之后自己看了看,效果不错,同样eoe社区也有很多相关的文章,比如http://www.eoeandroid.com/forum.php?mod=viewthread&tid=157771&page=21#pid1384160,大家可以看看,使用viewpager的时候大家不要忘了导入android-support-v4.jar这个包,自己可以去下载。 但是在使用的时候发现以上找到的viewpager不能实现循环滑动,这对于用户体验可能不是太好,所以自己又开始在此基础上寻找其他的方法,最终发现了以下解决办法: 将MyPagerAdapter修改一下:
004 | public class MyPagerAdapter extends PagerAdapter { |
005 | public List<View> views; |
008 | public MyPagerAdapter(Context context,List<View> views) { |
010 | this.context=context; |
011 | mCount = views.size(); |
014 | public void destroyItem(View collection, int position, Object arg2) { |
015 | if (position >= views.size()) { |
016 | int newPosition = position%views.size(); |
017 | position = newPosition; |
018 | // ((ViewPager) collection).removeView(views.get(position)); |
021 | position = -position; |
022 | // ((ViewPager) collection).removeView(views.get(position)); |
026 | public void finishUpdate(View arg0) { |
029 | public int getCount() { |
030 | return mCount+1;//此处+1才能向右连续滚动 |
033 | public Object instantiateItem(View collection, int position) { |
034 | if (position >= views.size()) { |
035 | int newPosition = position%views.size(); |
037 | position = newPosition; |
041 | position = -position; |
045 | ((ViewPager) collection).addView(views.get(position), 0); |
046 | } catch (Exception e) { |
048 | return views.get(position); |
051 | public boolean isViewFromObject(View view, Object object) { |
052 | return view == (object); |
055 | public void restoreState(Parcelable arg0, ClassLoader arg1) { |
058 | public Parcelable saveState() { |
062 | public void startUpdate(View arg0) { |
065 | 同时如果你要的效果里面有上面链接中的那个白色的动画效果,同样也需要再修改一个地方 |
069 | public class MyOnPageChangeListener implements OnPageChangeListener { |
070 | int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量 |
071 | int two = one * 2;// 页卡1 -> 页卡3 偏移量 |
073 | public void onPageSelected(int arg0) { |
074 | Animation animation = null; |
080 | if (currIndex == 1) { |
081 | animation = new TranslateAnimation(one, 0, 0, 0); |
082 | } else if (currIndex == 2) { |
083 | animation = new TranslateAnimation(two, 0, 0, 0); |
087 | if (currIndex == 0) { |
088 | animation = new TranslateAnimation(offset, one, 0, 0); |
089 | } else if (currIndex == 2) { |
090 | animation = new TranslateAnimation(two, one, 0, 0); |
094 | if (currIndex == 0) { |
095 | animation = new TranslateAnimation(offset, two, 0, 0); |
096 | } else if (currIndex == 1) { |
097 | animation = new TranslateAnimation(one, two, 0, 0); |
102 | animation.setFillAfter(true);// True:图片停在动画结束位置 |
103 | animation.setDuration(300); |
104 | cursor.startAnimation(animation); |
107 | public void onPageScrolled(int arg0, float arg1, int arg2) { |
110 | public void onPageScrollStateChanged(int arg0) { |
这样一来,其他地方不需要修改,即可实现viewpager的循环滑动效果
自己继续修改了以下,发现可以实现向左无限循环(貌似是无限)的,我的方法如下 有几个方法做以下改动就行了
02 | public void destroyItem(View collection, int position, Object arg2) { |
04 | // ((ViewPager) collection).removeView(views.get(position%views.size())); |
08 | public void finishUpdate(View arg0) { |
12 | public int getCount() { |
13 | return Integer.MAX_VALUE;//设置成最大值以便循环滑动 |
17 | public Object instantiateItem(View collection, int position) { |
19 | ((ViewPager) collection).addView(views.get(position%views.size()), 0); |
20 | } catch (Exception e) { |
22 | return views.get(position%views.size()); |
最后再初始化页面时mPager.setCurrentItem(3*100);//设置初始页面,为0的话开始不能向左滑动
这样的话就能实现类似的无限循环(向左其实只有100次,只是一般没人会在那儿向左移动那么多次而已)
涉及ViewPager更新问题
ViewPager的PagerAdapter不可以更新数据
在做项目的时候,发现即使调用了galleryAdapter.notifyDataSetChanged();
但是ViewPager还是不会更新原来的数据。
后来在stackoverflow上面找到了方法,原文链接:
http://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view
于是花了一点时间,修改了代码:
01 | protected PagerAdapter galleryAdapter = new PagerAdapter() { |
04 | public boolean isViewFromObject(View arg0, Object arg1) { |
05 | return arg0 == ((View)arg1); |
09 | public int getCount() { |
14 | public Object instantiateItem(View container, int position) { |
15 | return bindGalleryAdapterItemView(container, position); |
19 | public void destroyItem(View container, int position, Object object) { |
20 | ((ViewPager) container).removeView((View) object); |
24 | public void finishUpdate(View arg0) {} |
27 | public void restoreState(android.os.Parcelable state, ClassLoader loader) { |
32 | public Parcelable saveState() { |
37 | public void startUpdate(View arg0) {} |
40 | public int getItemPosition(Object object) { |
注意:POSITION_NONE 是一个PagerAdapter的内部常量,值是-2, API里面有说明: int android.support.v4.view.PagerAdapter.POSITION_NONE = -2 [0xfffffffe] 可以更新数据了。嘿嘿。