在网上看到了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的循环滑动效果 php
本身继续修改了如下,发现能够实现向左无限循环(貌似是无限)的,个人方法以下 有几个方法作如下改动就好了 android
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次,只是通常没人会在那儿向左移动那么屡次而已) ide
涉及ViewPager更新问题 动画
ViewPager的PagerAdapter不能够更新数据 this
在作项目的时候,发现即便调用了galleryAdapter.notifyDataSetChanged(); spa
可是ViewPager仍是不会更新原来的数据。 .net
后来在stackoverflow上面找到了方法,原文连接: rest
http://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view orm
因而花了一点时间,修改了代码: blog
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] 能够更新数据了。嘿嘿。