1 package com.bwei.admin.viewpagerdemo; 2 3 import android.os.Bundle; 4 import android.os.Handler; 5 import android.os.Message; 6 import android.support.v4.view.PagerAdapter; 7 import android.support.v4.view.ViewPager; 8 import android.support.v7.app.AppCompatActivity; 9 import android.view.View; 10 import android.view.ViewGroup; 11 import android.widget.ImageView; 12 13 import com.bumptech.glide.Glide; 14 15 public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 16 17 private ViewPager mViewPager; 18 25 private String [] images={"http://l2.51fanli.net//tuan//images//1//5806eac956808.jpg", 26 "http://l2.51fanli.net//tuan//images//b//580991bb30560.jpg", 27 "http://l0.51fanli.net//tuan//images//b//58115f2593dc3.jpg", 28 "http://l2.51fanli.net//tuan//images//0//57923840b054d.jpg", 29 "http://l2.51fanli.net//tuan//images//e//58101e11ab164.jpg"}; 30 private ImageView[] mImageViews; 31 private int index = 0; 32 private Handler handler = new Handler(){ 33 @Override 34 public void handleMessage(Message msg) { 35 index = index%images.length; 36 mViewPager.setCurrentItem(index,false); 37 index++; 38 index = index%images.length; 39 //handler.sendEmptyMessageDelayed(index,2000); 40 send(); 41 42 } 43 }; 44 @Override 45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 setContentView(R.layout.activity_main); 48 49 initView(); 50 mImageViews = new ImageView[5]; 51 mImageViews[0] = (ImageView) findViewById(R.id.point1); 52 mImageViews[1] = (ImageView) findViewById(R.id.point2); 53 mImageViews[2] = (ImageView) findViewById(R.id.point3); 54 mImageViews[3] = (ImageView) findViewById(R.id.point4); 55 mImageViews[4] = (ImageView) findViewById(R.id.point5); 56 57 for (ImageView images: mImageViews) { 58 images.setOnClickListener(this); 59 } 60 updateView(index); 61 //handler.sendEmptyMessageDelayed(index,2000); 62 send(); 63 } 64 65 public void send(){ 66 new Thread(){ 67 @Override 68 public void run() { 69 handler.sendEmptyMessageDelayed(index,2000); 70 } 71 }.start(); 72 } 73 74 private void initView() { 75 mViewPager = (ViewPager) findViewById(R.id.viewPager); 76 MyPageAdapter adapter = new MyPageAdapter(); 77 mViewPager.setAdapter(adapter); 78 79 80 mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 81 @Override 82 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 83 84 } 85 86 @Override 87 public void onPageSelected(int position) { 88 updateView(position); 89 } 90 91 @Override 92 public void onPageScrollStateChanged(int state) { 93 94 } 95 }); 96 } 97 98 //图片切换更新 99 private void updateView(int position) { 100 //首先初始化成未选中的样子 101 for (ImageView images: mImageViews) { 102 images.setImageResource(R.drawable.nor); 103 } 104 //将选中的positon的更改为选中的样子 105 mImageViews[position].setImageResource(R.drawable.yer); 106 } 107 108 @Override 109 public void onClick(View v) { 110 handler.removeCallbacksAndMessages(null); 111 int index1 = Integer.parseInt(v.getTag().toString()); 112 mViewPager.setCurrentItem(index1,false); 113 index = index1+1; 114 send(); 115 } 116 117 class MyPageAdapter extends PagerAdapter{ 118 119 @Override 120 public int getCount() { 121 return images.length; 122 } 123 124 @Override 125 public boolean isViewFromObject(View view, Object object) { 126 return view==object; 127 } 128 129 @Override 130 public Object instantiateItem(ViewGroup container, int position) { 131 ImageView imageView = new ImageView(MainActivity.this); 132 // imageView.setImageResource(imageId[position%imageId.length]); 133 Glide.with(MainActivity.this).load(images[position]).placeholder(R.drawable.yer).into(imageView); 134 container.addView(imageView); 135 return imageView; 136 } 137 138 @Override 139 public void destroyItem(ViewGroup container, int position, Object object) { 140 //super.destroyItem(container, position, object); 141 container.removeView((View)object); 142 } 143 } 144 145 }
http://blog.csdn.net/ss1168805219/article/details/52282750(纤细参考)
时间: 2024-12-19 10:53:52