前言
网上有很多使用ViewPager实现左右滑动这一效果的资料,这些资料大多数都是将PagerAdapter中getCount()方法的返回值设为Integer.MAX_VALUE使用户看不到边界,然后在instantiateItem()方法中通过position%(要循环显示的数据集的长度)的方式取得对应的数据集。这样虽然可以做到无限循环,但是会有两个弊端:首先会创建大量对象,容易引起内存溢出(循环加载图片)从而影响性能;其次从第一页向右滑动的时候是无法滑动的。
今天给大家分享的是另一种实现方式:创建三个图片视图放入ViewPager中默认选中第二页,在接下来的滑动中每次滑动结束之后都将当前页码设置为第二页,然后通过判断是向左向右滑动来设置视图中的数据集,这样就可以实现无限循环了,而且只会创建三个图片视图对象。
示例源码
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
package com.viewpager; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.app.Activity; public class MainActivity01 extends Activity implements OnPageChangeListener { private ViewPager viewPager; static final int arrays[] = { R.drawable.guide1, R.drawable.guide2, R.drawable.guide3, R.drawable.guide4, R.drawable.guide5 }; private List<ImageView> views; private int currentPage = 0 ; private ImageView imageView; private MyViewPagerAdapter viewPagerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); initWidget(); } private void initWidget() { viewPager = (ViewPager) findViewById(R.id.viewpager); views = new ArrayList<ImageView>(); for ( int i = 0 ; i < 3 ; i++) { imageView = new ImageView( this ); imageView.setLayoutParams( new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); views.add(imageView); } initImageData(); viewPagerAdapter = new MyViewPagerAdapter(); viewPager.setAdapter(viewPagerAdapter); viewPager.setCurrentItem( 1 ); viewPager.setOnPageChangeListener( this ); } private void initImageData() { for ( int i = 0 ; i < 3 ; i++) { imageView = views.get(i); if (i == 0 ) { imageView.setImageResource(arrays[arrays.length - 1 ]); } else { imageView.setImageResource(arrays[i - 1 ]); } } } class MyViewPagerAdapter extends PagerAdapter { @Override public int getCount() { // TODO Auto-generated method stub return views.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public Object instantiateItem(ViewGroup container, int position) { imageView = views.get(position); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView((View) object); } } // 当滑动状态改变时调用 @Override public void onPageScrollStateChanged( int state) { // TODO Auto-generated method stub System.out.println( "--onPageScrollStateChanged--state--:" + state); switch (state) { // 在滚动完成后 case ViewPager.SCROLL_STATE_IDLE: int currentItem = viewPager.getCurrentItem(); System.out.println( "--currentItem--00--:" + currentItem); System.out.println( "--currentPage--00--:" + currentPage); if (viewPager.getCurrentItem() == 1 ) { // 如果位置没有变终止循环 break ; } if (viewPager.getCurrentItem() > 1 ) { currentPage++; } else { currentPage--; } System.out.println( "--currentPage--11--:" + currentPage); if (currentPage == arrays.length) { currentPage = 0 ; } if (currentPage == - 1 ) { currentPage = arrays.length - 1 ; } System.out.println( "--currentPage--22--:" + currentPage); if (currentPage == 0 ) { views.get( 0 ).setImageResource(arrays[arrays.length - 1 ]); } else { views.get( 0 ).setImageResource(arrays[currentPage - 1 ]); } views.get( 1 ).setImageResource(arrays[currentPage]); if (currentPage == arrays.length - 1 ) { views.get( 2 ).setImageResource(arrays[ 0 ]); } else { views.get( 2 ).setImageResource(arrays[currentPage + 1 ]); } viewPager.setCurrentItem( 1 , false ); currentItem = viewPager.getCurrentItem(); System.out.println( "--currentItem--11--:" + currentItem); break ; } } // 当当前页面被滑动时调用 @Override public void onPageScrolled( int position, float positionOffset, int positionOffsetPixels) { // TODO Auto-generated method stub // System.out.println("--onPageScrolled--position--:" + position); } // 当新的页面被选中时调用 @Override public void onPageSelected( int position) { // TODO Auto-generated method stub System.out.println( "--onPageSelected--position--:" + position); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u013693649/article/details/52199729