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 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 stub52 //引导3个ViewPager
53 mImageViewList = new ArrayList();
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 //@Override120 //public void destroyItem(View container, int position, Object object)121 //{122 // //TODO Auto-generated method stub123 //((ViewGroup) container).removeView(images[position124 //% 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 *@authorAdministrator168 *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 *@authorAdministrator207 *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 }