布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="match_parent" android:layout_height="200dp" > <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:id="@+id/ll_point" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerInParent="true" android:layout_marginBottom="20dp" android:orientation="horizontal" /> </RelativeLayout> </RelativeLayout>
相关代码
1 public class MainActivity extends Activity { 2 3 private ViewPager mViewPager; 4 private LinearLayout mLlPoint; 5 private MyAdapter mAdapter; 6 7 private int[] ids = {R.drawable.a1,R.drawable.a2,R.drawable.a3}; 8 private List<ImageView> mImages; 9 private int lastPos; 10 private boolean isRunning; 11 12 private Handler mHandler = new Handler() { 13 public void handleMessage(android.os.Message msg) { 14 switch(msg.what){ 15 case 0: 16 17 mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1); 18 19 if (isRunning) { 20 mHandler.sendEmptyMessageDelayed(0,3000); 21 } 22 23 break; 24 25 default: 26 break; 27 } 28 }; 29 }; 30 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_main); 35 36 initView(); 37 initData(); 38 39 mAdapter = new MyAdapter(); 40 mViewPager.setAdapter(mAdapter); 41 mViewPager.addOnPageChangeListener(new PagerChnageListener()); 42 mViewPager.setCurrentItem((mImages.size()) * 1000); 43 44 isRunning = true; 45 mHandler.sendEmptyMessageDelayed(0,3000); 46 } 47 48 private void initView() { 49 mViewPager = (ViewPager)findViewById(R.id.vp); 50 mLlPoint = (LinearLayout)findViewById(R.id.ll_point); 51 } 52 53 private void initData() { 54 55 mImages = new ArrayList<ImageView>(); 56 57 for (int i = 0;i < ids.length;i++) { 58 ImageView iv = new ImageView(this); 59 iv.setBackgroundResource(ids[i]); 60 mImages.add(iv); 61 62 View view = new View(this); 63 view.setBackgroundResource(R.drawable.point_selector); 64 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Utils.dip2px(this,10), 65 Utils.dip2px(this,10)); 66 if (i != 0) { 67 params.leftMargin = 20; 68 view.setEnabled(false); 69 }else { 70 view.setEnabled(true); 71 } 72 view.setLayoutParams(params); 73 mLlPoint.addView(view); 74 } 75 } 76 77 class MyAdapter extends PagerAdapter { 78 79 @Override 80 public int getCount() { 81 return Integer.MAX_VALUE; 82 } 83 84 @Override 85 public boolean isViewFromObject(View arg0,Object arg1) { 86 return arg0 == arg1; 87 } 88 89 @Override 90 public Object instantiateItem(ViewGroup container,int position) { 91 ImageView view = mImages.get(position % mImages.size()); 92 93 // 增加异常扑捉防止空白页 94 try { 95 ((ViewPager)container).addView(view,0); 96 }catch(Exception e) { 97 e.printStackTrace(); 98 } 99 return view; 100 } 101 102 @Override 103 public void destroyItem(ViewGroup container,int position,Object object) { 104 // 防止2/3个出现BUG 105 // container.removeView(mImages.get(position % mImages.size())); 106 } 107 } 108 109 class PagerChnageListener implements OnPageChangeListener { 110 111 /* state: 0空闲,1是滑行中,2加载完毕 */ 112 @Override 113 public void onPageScrollStateChanged(int state) { 114 switch(state){ 115 case 0: 116 isRunning = true; 117 break; 118 case 1: 119 isRunning = false; 120 break; 121 case 2: 122 isRunning = true; 123 break; 124 125 default: 126 break; 127 } 128 } 129 130 @Override 131 public void onPageScrolled(int position,float positionOffset,int positionOffsetPixels) { 132 133 } 134 135 @Override 136 public void onPageSelected(int position) { 137 int pos = position % mImages.size(); 138 mLlPoint.getChildAt(lastPos).setEnabled(false); 139 mLlPoint.getChildAt(pos).setEnabled(true); 140 lastPos = pos; 141 } 142 } 143 144 @Override 145 protected void onDestroy() { 146 super.onDestroy(); 147 isRunning = false; 148 mHandler.removeCallbacksAndMessages(null); 149 } 150 }
效果图