1 package com.itheima27.viewpagerdemo; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.app.Activity; 7 import android.os.Bundle; 8 import android.os.SystemClock; 9 import android.support.v4.view.PagerAdapter; 10 import android.support.v4.view.ViewPager; 11 import android.support.v4.view.ViewPager.OnPageChangeListener; 12 import android.view.View; 13 import android.view.ViewGroup; 14 import android.widget.ImageView; 15 import android.widget.LinearLayout; 16 import android.widget.LinearLayout.LayoutParams; 17 import android.widget.TextView; 18 19 public class MainActivity extends Activity implements OnPageChangeListener { 20 21 private List<ImageView> imageViewList; 22 private TextView tvDescription; 23 private LinearLayout llPointGroup; 24 private int previousEnabledPosition = 0; // 前一个被选中的"点"的索引, 默认情况下: 0 25 26 private String[] imageDescriptionArray = { 27 "巩俐不低俗,我就不能低俗", 28 "扑树又回来啦!再唱经典老歌引万人大合唱", 29 "揭秘北京电影如何升级", 30 "乐视网TV版大派送", 31 "热血屌丝的反杀" 32 }; 33 private ViewPager mViewPager; 34 35 private boolean isStop = false; // 是否停止子线程, 不会停止 36 37 38 @Override 39 protected void onDestroy() { 40 isStop = true; 41 super.onDestroy(); 42 } 43 44 @Override 45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 setContentView(R.layout.activity_main); 48 49 init(); 50 51 // 开启线程无限自动移动 52 Thread mThread = new Thread(new Runnable() { 53 @Override 54 public void run() { 55 while(!isStop) { 56 // 每隔2秒钟, 发送一条消息到主线程, 更新viewpager的界面 57 SystemClock.sleep(2000); 58 59 runOnUiThread(new Runnable() { 60 @Override 61 public void run() { 62 // 此方法是在主线程中执行 63 mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1); 64 } 65 }); 66 } 67 } 68 }); 69 mThread.start(); 70 } 71 72 private void init() { 73 mViewPager = (ViewPager) findViewById(R.id.viewpager); 74 llPointGroup = (LinearLayout) findViewById(R.id.ll_point_group); 75 tvDescription = (TextView) findViewById(R.id.tv_image_description); 76 77 imageViewList = new ArrayList<ImageView>(); 78 79 int[] imageResIDs = { 80 R.drawable.a, 81 R.drawable.b, 82 R.drawable.c, 83 R.drawable.d, 84 R.drawable.e 85 }; 86 87 ImageView iv; 88 View view; 89 LayoutParams params; 90 for (int id : imageResIDs) { 91 iv = new ImageView(this); 92 iv.setBackgroundResource(id); 93 imageViewList.add(iv); 94 95 96 // 每循环一次, 添加一个点到Linearlayout中 97 view = new View(this); 98 view.setBackgroundResource(R.drawable.point_background); 99 params = new LayoutParams(5, 5); 100 params.leftMargin = 5; 101 view.setEnabled(false); 102 view.setLayoutParams(params); 103 104 llPointGroup.addView(view); // 向线性布局中添加"点" 105 } 106 107 mViewPager.setAdapter(new MyAdapter()); 108 mViewPager.setOnPageChangeListener(this); 109 110 // 初始化图片描述, 和哪一个点被选中 111 // tvDescription.setText(imageDescriptionArray[0]); 112 // llPointGroup.getChildAt(0).setEnabled(true); 113 114 115 // 初始化ViewPager的默认position为Integer.Max_value的一半 116 int index = (Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2 % imageViewList.size()); 117 mViewPager.setCurrentItem(index); // 设置当前viewpager选中的pager页, 会触发OnPageChangeListener.onPagerSelected 118 119 } 120 121 class MyAdapter extends PagerAdapter { 122 123 @Override 124 public int getCount() { 125 return Integer.MAX_VALUE; 126 } 127 128 /** 129 * 复用对象. 130 * true 复用对象 131 * false 用的是object 132 */ 133 @Override 134 public boolean isViewFromObject(View arg0, Object arg1) { 135 return arg0 == arg1; 136 } 137 138 /** 139 * 销毁对象 140 * @param position 将要被销毁对象的索引位置 141 */ 142 @Override 143 public void destroyItem(ViewGroup container, int position, Object object) { 144 container.removeView(imageViewList.get(position % imageViewList.size())); 145 } 146 147 /** 148 * 初始化一个view对象 149 * @param position 将要被创建的对象的索引位置 150 */ 151 @Override 152 public Object instantiateItem(ViewGroup container, int position) { 153 // 先把对象添加到viewpager中, 再返回当前对象 154 container.addView(imageViewList.get(position % imageViewList.size())); 155 return imageViewList.get(position % imageViewList.size()); 156 } 157 } 158 159 @Override 160 public void onPageScrollStateChanged(int arg0) { 161 // TODO Auto-generated method stub 162 163 } 164 165 @Override 166 public void onPageScrolled(int arg0, float arg1, int arg2) { 167 // TODO Auto-generated method stub 168 169 } 170 171 @Override 172 public void onPageSelected(int position) { 173 174 // 取余后的索引 175 int newPosition = position % imageViewList.size(); 176 177 // 根据索引设置图片的描述 178 tvDescription.setText(imageDescriptionArray[newPosition]); 179 180 // 把上一个点设置为未选中 181 llPointGroup.getChildAt(previousEnabledPosition).setEnabled(false); 182 183 // 根据索引设置哪一个点被选中 184 llPointGroup.getChildAt(newPosition).setEnabled(true); 185 186 previousEnabledPosition = newPosition; 187 } 188 }