android實現無限輪播 -开发者知识库

android實現無限輪播 -开发者知识库,第1张

1 在ViewPager的適配器中的getCount()長度設置無限大Integer.MAX_VALUE

2  明白當前currentIten 為position % images.length;

3  設置一開始ViewPager的位置 viewPager.setCurrentItem((images.length) * 1000);這樣剛開始就可以向左滑動了

4  在OnPageChangeListener ViewPager的滑動監聽中設置小點的位置

5  設置Handler 定時滑動開啟滑動

6 在onResume中發送空消息,在onPause中移除消息將所有的Callbacks和Messages全部清除掉,避免內存泄露。

如下是主代碼 圖片是本地的

  1 package com.demo.sb.main;
2
3 import java.util.ArrayList;
4 import com.demo.sb.utils.DensityUtil;
5 import com.demo.suibian.R;
6 import android.annotation.SuppressLint;
7 import android.app.Activity;
8 import android.os.Bundle;
9 import android.os.Handler;
10 import android.support.v4.view.PagerAdapter;
11 import android.support.v4.view.ViewPager;
12 import android.support.v4.view.ViewPager.OnPageChangeListener;
13 import android.view.MotionEvent;
14 import android.view.View;
15 import android.view.View.OnClickListener;
16 import android.view.View.OnTouchListener;
17 import android.view.ViewGroup;
18 import android.widget.ImageView;
19 import android.widget.LinearLayout;
20
21 public class MActivity_Luenbo extends Activity {
22
23 private ViewPager viewPager;
24 private LinearLayout group;
25 private int[] images = { R.drawable.a, R.drawable.c, R.drawable.d,
26 R.drawable.guide_1, R.drawable.guide_2 };
27 private ArrayList<ImageView> mImageViewList;
28 private ImageView[] diandian;// ViewPager上的點點
29 private int selectedItem = 0;
30
31 @Override
32 protected void onCreate(Bundle savedInstanceState) {
33 // TODO Auto-generated method stub
34 super.onCreate(savedInstanceState);
35 setContentView(R.layout.mactivity_luenbo);
36 viewPager = (ViewPager) findViewById(R.id.vp_viewPager);
37 group = (LinearLayout) findViewById(R.id.ll_viewGroup);
38 init();
39
40 }
41
42 @SuppressLint("HandlerLeak")
43 private Handler mHandler = new Handler() {
44 public void handleMessage(android.os.Message msg) {
45 viewPager.setCurrentItem(selectedItem 1);
46 mHandler.sendEmptyMessageDelayed(0, 3000);
47 };
48 };
49
50 private void init() {
51 // TODO Auto-generated method stub
52 // 引導3個ViewPager
53 mImageViewList = new ArrayList<ImageView>();
54 for (int i = 0; i < images.length; i ) {
55 ImageView imageView = new ImageView(this);
56 imageView.setBackgroundResource(images[i]);
57 mImageViewList.add(imageView);
58 }
59
60 // 將點點加入到ViewGroup中
61 diandian = new ImageView[images.length];
62 for (int i = 0; i < images.length; i ) {
63 // 循環加入點點圖片組
64 diandian[i] = new ImageView(this);
65 if (i == 0) {
66 diandian[i].setImageResource(R.drawable.page_indicator_focused);
67 } else {
68 diandian[i]
69 .setImageResource(R.drawable.page_indicator_unfocused);
70 }
71 diandian[i].setPadding(8, 8, 8, 8);
72 group.addView(diandian[i]);
73 }
74 viewPager.setAdapter(new GuideAdapter());
75 // 設置監聽,主要是設置點點的背景
76 viewPager.setOnPageChangeListener(new GuidePagerListener());
77 // 設置ViewPager的默認項,設置為長度的1000倍,這樣開始就能向左滑動了
78 viewPager.setCurrentItem((images.length) * 1000);
79 }
80
81 /**
82 * 每次當onResume有焦點時發送個空消息開始輪播
83 */
84 @Override
85 protected void onResume() {
86 // TODO Auto-generated method stub
87 super.onResume();
88 mHandler.sendEmptyMessageDelayed(0, 2000);
89 }
90
91 /**
92 * 當暫停時停止輪播
93 */
94 @Override
95 protected void onPause() {
96 // TODO Auto-generated method stub
97 super.onPause();
98 mHandler.removeCallbacksAndMessages(null);
99 }
100
101 /**
102 * ViewPager的適配器
103 *
104 */
105 private class GuideAdapter extends PagerAdapter {
106
107 @Override
108 public int getCount() {
109 // TODO Auto-generated method stub
110 return Integer.MAX_VALUE;
111 }
112
113 @Override
114 public boolean isViewFromObject(View arg0, Object arg1) {
115 // TODO Auto-generated method stub
116 return arg0 == arg1;
117 }
118
119 // @Override
120 // public void destroyItem(View container, int position, Object object)
121 // {
122 // // TODO Auto-generated method stub
123 // ((ViewGroup) container).removeView(images[position
124 // % images.length]);
125 // }
126 @Override
127 public void destroyItem(ViewGroup container, int position, Object object) {
128 // TODO Auto-generated method stub
129 container.removeView((View) object);
130 }
131
132 /**
133 * 加載圖片進去,用當前的position除以圖片數組長度取余數是關鍵
134 */
135 @Override
136 public Object instantiateItem(ViewGroup container, int position) {
137 // TODO Auto-generated method stub
138 final int currentIten = position % images.length;
139 ImageView iView = mImageViewList.get(currentIten);
140 try {
141
142 if (iView.getParent() == null) {
143 container.addView(iView);
144 }
145 iView.setOnClickListener(new OnClickListener() {
146
147 @Override
148 public void onClick(View arg0) {
149 // TODO Auto-generated method stub
150 DensityUtil.showToast(MActivity_Luenbo.this,
151 currentIten "");
152 }
153 });
154 iView.setOnTouchListener(new NewTouchListener());
155 } catch (Exception e) {
156 // TODO: handle exception
157 }
158
159 return iView;
160 }
161
162 }
163
164 /**
165 * ViewPager 的滑動監聽
166 *
167 * @author Administrator
168 *
169 */
170 private class GuidePagerListener implements OnPageChangeListener {
171
172 @Override
173 public void onPageScrollStateChanged(int arg0) {
174 // TODO Auto-generated method stub
175
176 }
177
178 @Override
179 public void onPageScrolled(int arg0, float arg1, int arg2) {
180 // TODO Auto-generated method stub
181 System.out.println(arg0 " dd " arg1 " dddd " arg2);
182 }
183
184 @Override
185 public void onPageSelected(int position) {
186 // TODO Auto-generated method stub
187 selectedItem = position;
188 System.out.println("第幾個頁面" position " " position
189 % mImageViewList.size());
190 for (int i = 0; i < diandian.length; i ) {
191 if (i == position % images.length) {
192 diandian[i]
193 .setImageResource(R.drawable.page_indicator_focused);
194 } else {
195 diandian[i]
196 .setImageResource(R.drawable.page_indicator_unfocused);
197 }
198 }
199 }
200
201 }
202
203 /**
204 * ViewPager的觸摸事件當按下圖片停止輪播
205 *
206 * @author Administrator
207 *
208 */
209 private class NewTouchListener implements OnTouchListener {
210
211 @Override
212 public boolean onTouch(View arg0, MotionEvent arg1) {
213 // TODO Auto-generated method stub
214 switch (arg1.getAction()) {
215 case MotionEvent.ACTION_DOWN:
216 mHandler.removeCallbacksAndMessages(null);
217 break;
218 case MotionEvent.ACTION_CANCEL:
219 mHandler.sendEmptyMessageDelayed(0, 3000);
220 break;
221 case MotionEvent.ACTION_UP:
222 mHandler.sendEmptyMessageDelayed(0, 3000);
223 break;
224 default:
225 break;
226 }
227 return true;
228 }
229
230 }
231
232 }
 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:layout_width="match_parent"
9 android:layout_height="wrap_content"
10 android:padding="10dp"
11 android:text="@string/luenbo"
12 android:textColor="#f00" />
13
14 <FrameLayout
15 android:layout_width="match_parent"
16 android:layout_height="200dp" >
17
18 <android.support.v4.view.ViewPager
19 android:id="@ id/vp_viewPager"
20 android:layout_width="match_parent"
21 android:layout_height="match_parent" />
22
23 <RelativeLayout
24 android:layout_width="wrap_content"
25 android:layout_height="wrap_content"
26 android:layout_gravity="bottom|center"
27 android:layout_marginBottom="20dp" >
28
29 <LinearLayout
30 android:id="@ id/ll_viewGroup"
31 android:layout_width="wrap_content"
32 android:layout_height="wrap_content"
33 android:orientation="horizontal" />
34 </RelativeLayout>
35
36 </FrameLayout>
37
38 </LinearLayout>

 

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复