1 packagecom.example.testa;2
3 importandroid.support.v4.view.PagerAdapter;4 importandroid.support.v4.view.ViewPager;5 importandroid.support.v4.view.ViewPager.OnPageChangeListener;6 importandroid.app.Activity;7 importandroid.os.Bundle;8 importandroid.view.View;9 importandroid.view.ViewGroup;10 importandroid.view.ViewGroup.LayoutParams;11 importandroid.widget.ImageView;12 importandroid.widget.LinearLayout;13
14 public class MainActivity extends Activity implementsOnPageChangeListener {15
16
19 privateViewPager viewPager;20
21
24 privateImageView[] tips;25
26
29 privateImageView[] mImageViews;30
31
34 private int[] imgIdArray;35
36 @Override37 protected voidonCreate(Bundle savedInstanceState) {38 super.onCreate(savedInstanceState);39 setContentView(R.layout.activity_main);40 ViewGroup group =(ViewGroup) findViewById(R.id.viewGroup);41 viewPager =(ViewPager) findViewById(R.id.viewPager);42
43 //载入图片资源ID
44 imgIdArray = new int[] { R.drawable.item01, R.drawable.item02,45 R.drawable.item03, R.drawable.item04, R.drawable.item05,46 R.drawable.item06, R.drawable.item07, R.drawable.item08 };47
48 //将点点加入到ViewGroup中
49 tips = newImageView[imgIdArray.length];50 for (int i = 0; i < tips.length; i++) {51 ImageView imageView = new ImageView(this);52 imageView.setLayoutParams(new LayoutParams(10, 10));53 tips[i] =imageView;54 if (i == 0) {55 tips[i].setBackgroundResource(R.drawable.page_indicator_focused);56 } else{57 tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);58 }59
60 LinearLayout.LayoutParams layoutParams = newLinearLayout.LayoutParams(61 newViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,62 LayoutParams.WRAP_CONTENT));63 layoutParams.leftMargin = 5;64 layoutParams.rightMargin = 5;65 group.addView(imageView, layoutParams);66 }67
68 //将图片装载到数组中
69 mImageViews = newImageView[imgIdArray.length];70 for (int i = 0; i < mImageViews.length; i++) {71 ImageView imageView = new ImageView(this);72 mImageViews[i] =imageView;73 imageView.setBackgroundResource(imgIdArray[i]);74 }75
76 //设置Adapter
77 viewPager.setAdapter(newMyAdapter());78 //设置监听,主要是设置点点的背景
79 viewPager.setOnPageChangeListener(this);80 //设置ViewPager的默认项, 设置为长度的100倍,这样子开始就能往左滑动
81 viewPager.setCurrentItem((mImageViews.length) * 100);82
83 }84
85
90 public class MyAdapter extendsPagerAdapter {91
92 @Override93 public intgetCount() {94 returnInteger.MAX_VALUE;95 }96
97 @Override98 public booleanisViewFromObject(View arg0, Object arg1) {99 return arg0 ==arg1;100 }101
102 @Override103 public void destroyItem(View container, intposition, Object object) {104 }105
106
109 @Override110 public Object instantiateItem(View container, intposition) {111 try{112 ((ViewPager)container).addView(mImageViews[position % mImageViews.length], 0);113 }catch(Exception e){114 //handler something
115 }116 return mImageViews[position %mImageViews.length];117
118 }119
120 }121
122 @Override123 public void onPageScrollStateChanged(intarg0) {124
125 }126
127 @Override128 public void onPageScrolled(int arg0, float arg1, intarg2) {129
130 }131
132 @Override133 public void onPageSelected(intarg0) {134 setImageBackground(arg0 %mImageViews.length);135 }136
137
142 private void setImageBackground(intselectItems) {143 for (int i = 0; i < tips.length; i++) {144 if (i ==selectItems) {145 tips[i].setBackgroundResource(R.drawable.page_indicator_focused);146 } else{147 tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);148 }149 }150 }151
152 }