ViewPager实现循环滑动功能说明

来源:互联网 发布:ps读取复合数据怎么办 编辑:程序博客网 时间:2024/04/30 11:50
大家可以看看,使用viewpager的时候大家不要忘了导入android-support-v4.jar这个包,自己可以去下载。 但是在使用的时候发现以上找到的viewpager不能实现循环滑动,这对于用户体验可能不是太好,所以自己又开始在此基础上寻找其他的方法,最终发现了以下解决办法: 将MyPagerAdapter修改一下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
  * ViewPager适配器
  */
publicclass MyPagerAdapter extendsPagerAdapter {
  publicList<View> views;
  Context context;
  intmCount;
  publicMyPagerAdapter(Context context,List<View> views) {
   this.views = views;
   this.context=context;  
         mCount = views.size();
  }
  @Override
  publicvoid destroyItem(View collection, intposition, Object arg2) {
   if(position >= views.size()) {
    intnewPosition = position%views.size();  
                position = newPosition;
//       ((ViewPager) collection).removeView(views.get(position));
   }
   if(position <0){
    position = -position;
//    ((ViewPager) collection).removeView(views.get(position));
   }
  }
  @Override
  publicvoid finishUpdate(View arg0) {
  }
  @Override
  publicint getCount() {
   returnmCount+1;//此处+1才能向右连续滚动
  }
  @Override
  publicObject instantiateItem(View collection, intposition) {
   if(position >= views.size()) {
    intnewPosition = position%views.size();
                
                position = newPosition;
                mCount++;
   }
   if(position <0){
    position = -position;
    mCount--;
   }
   try{
    ((ViewPager) collection).addView(views.get(position), 0);
   }catch(Exception e) {
         }
   returnviews.get(position);
  }
  @Override
  publicboolean isViewFromObject(View view, Object object) {
   returnview == (object);
  }
  @Override
  publicvoid restoreState(Parcelable arg0, ClassLoader arg1) {
  }
  @Override
  publicParcelable saveState() {
   returnnull;
  }
  @Override
  publicvoid startUpdate(View arg0) {
  }
}
同时如果你要的效果里面有上面链接中的那个白色的动画效果,同样也需要再修改一个地方
/**
  * 页卡切换监听,用于改变动画位置
  */
publicclass MyOnPageChangeListener implementsOnPageChangeListener {
  intone = offset * 2+ bmpW;// 页卡1 -> 页卡2 偏移量
  inttwo = one * 2;// 页卡1 -> 页卡3 偏移量
  @Override
  publicvoid onPageSelected(intarg0) {
   Animation animation = null;
   if(arg0>2){
    arg0=arg0%3;
   }
   switch(arg0) {
   case0:
    if(currIndex == 1) {
     animation = newTranslateAnimation(one, 0,0,0);
    }elseif (currIndex == 2) {
     animation = newTranslateAnimation(two, 0,0,0);
    }
    break;
   case1:
    if(currIndex == 0) {
     animation = newTranslateAnimation(offset, one, 0,0);
    }elseif (currIndex == 2) {
     animation = newTranslateAnimation(two, one, 0,0);
    }
    break;
   case2:
    if(currIndex == 0) {
     animation = newTranslateAnimation(offset, two, 0,0);
    }elseif (currIndex == 1) {
     animation = newTranslateAnimation(one, two, 0,0);
    }
    break;
   }
   currIndex = arg0;
   animation.setFillAfter(true);// True:图片停在动画结束位置
   animation.setDuration(300);
   cursor.startAnimation(animation);
  }
  @Override
  publicvoid onPageScrolled(intarg0, floatarg1, intarg2) {
  }
  @Override
  publicvoid onPageScrollStateChanged(intarg0) {
  }
}
这样一来,其他地方不需要修改,即可实现viewpager的循环滑动效果

自己继续修改了以下,发现可以实现向左无限循环(貌似是无限)的,我的方法如下 有几个方法做以下改动就行了
 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Override
                publicvoid destroyItem(View collection, intposition, Object arg2) {
                        //循环滑动时此处不能销毁
//                    ((ViewPager) collection).removeView(views.get(position%views.size()));
                }
 
                @Override
                publicvoid finishUpdate(View arg0) {
                }
 
                @Override
                publicint getCount() {
                        returnInteger.MAX_VALUE;//设置成最大值以便循环滑动
                }
 
                @Override
                publicObject instantiateItem(View collection, intposition) {
                        try{
                                ((ViewPager) collection).addView(views.get(position%views.size()), 0);
                        }catch(Exception e) {
                        }
                        returnviews.get(position%views.size());
                }

最后再初始化页面时mPager.setCurrentItem(3*100);//设置初始页面,为0的话开始不能向左滑动 

这样的话就能实现类似的无限循环(向左其实只有100次,只是一般没人会在那儿向左移动那么多次而已)


http://www.2cto.com/kf/201311/256446.html

0 0
原创粉丝点击