本文实例为大家分享了AnAndroid图片无限轮播的具体代码,供大家参考,具体内容如下
public class MainActivity extends Activity { private ViewPager viewPager; private LinearLayout ll_dot; private String[] imageUrls = new String[] { "http://pic8.nipic.com/20100701/5290458_114840036316_2.jpg", "http://pic2.nipic.com/20090424/1468853_230119053_2.jpg", "http://img3.3lian.com/2013/s1/20/d/57.jpg", "http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg", "http://a0.att.hudong.com/15/08/300218769736132194086202411_950.jpg" }; //private String[] imageUrls = new String[] { //"http://192.168.230.1:8080/ab.jpg", //"http://192.168.230.1:8080/ae.jpg", //"http://192.168.230.1:8080/af.jpg", //"http://192.168.230.1:8080/ah.jpg" }; private ArrayListviewList; private ArrayList dotList; Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { // 先去获取到当前条目索引 int currentItem = viewPager.getCurrentItem(); // 条目索引+1 currentItem++; // 重新设置给viewPager viewPager.setCurrentItem(currentItem); // 再调用发延时消息的方法 sendDelayMessage(); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(R.id.viewPager); ll_dot = (LinearLayout) findViewById(R.id.ll_dot); // 初始化图片 initViewList(); // 初始化小圆点 initDots(); // 设置数据适配器 viewPager.setAdapter(new MyPagerAdapter(this, viewList, handler)); // 设置初始的展示条目 viewPager.setCurrentItem(viewList.size() * 100000); // 设置延时切换 sendDelayMessage(); // 监听viewPager的一个滑动事件 viewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { // 遍历小点的集合 for (int i = 0; i (); dotList.clear(); for (int i = 0; i (); for (int i = 0; i
适配器
public class MyPagerAdapter extends PagerAdapter { private ListviewList; private Context context; private Handler handler; public MyPagerAdapter(Context context, List viewList, Handler handler) { this.cOntext= context; this.viewList = viewList; this.handler = handler; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public Object instantiateItem(ViewGroup container, int position) { // 通过控件集合获取imageView 添加到容器中,并作为key 返回 ImageView imageView = viewList.get(position % viewList.size()); imageView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { // 当按下的时候,停止轮播 case MotionEvent.ACTION_DOWN: // 移除所有的回调和消息 handler.removeCallbacksAndMessages(null); break; // 抬起 case MotionEvent.ACTION_UP: handler.sendEmptyMessageDelayed(0, 2000); break; // 取消的时候,继续开始录播 case MotionEvent.ACTION_CANCEL: handler.sendEmptyMessageDelayed(0, 2000); break; default: break; } //触摸事件不被消费,true 消费该触摸事件 return true; } }); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // super.destroyItem(container, position, object); container.removeView((View) object); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。