package com.example.wisdom.mylunbo; import android.content.Intent; import android.os.Handler; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import android.widget.LinearLayout; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { // String[] urls = BbImages.imgs; // 记录小圆点个数的容器 List<ImageView> dots = new ArrayList<ImageView>(); // List<Integer> list; private List<ImageView> list=new ArrayList<ImageView>();;// 存放要显示在ViewPager对象中的所有Imageview对象 private ViewPager vp; private LinearLayout ll_dot; Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { super.handleMessage(msg); // vp.setCurrentItem(vp.getCurrentItem()+1); int currentItem=vp.getCurrentItem(); currentItem++; vp.setCurrentItem(currentItem); // 发送延迟消息 this.sendEmptyMessageDelayed(0, 2000); } }; private int[] images; private static final int FIRST_PAGE = 1; private int currentPosition; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vp = (ViewPager) findViewById(R.id.vp); ll_dot = (LinearLayout) findViewById(R.id.ll_dot); images=init(); initdot(); vp.setAdapter(new VPAdapter(images, this, handler)); // handler.sendEmptyMessageDelayed(1, 3000); vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int postion) { for (int i = 0; i < dots.size(); i++) { ImageView img = dots.get(i); if (i == postion % dots.size()) { img.setImageResource(R.drawable.dot_focus); } else { img.setImageResource(R.drawable.dot_normal); } } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); vp.setCurrentItem(images.length*1000); handler.sendEmptyMessageDelayed(0, 2000); } private int[] init() { return new int[]{R.drawable.a,R.drawable.b,R.drawable.xiangqing,R.drawable.a,R.drawable.b,R.drawable.xiangqing }; } @Override protected void onStop() { super.onStop(); //移除所有消息和任务 handler.removeCallbacksAndMessages(null); } private void initdot() { for (int i = 0; i < images.length; i++) { ImageView img = new ImageView(this); // 现在空 if (i == 0) { img.setImageResource(R.drawable.dot_focus); } else { img.setImageResource(R.drawable.dot_normal); } LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20); /** * */ params.setMargins(5, 0, 5, 5); // 加载到布局容器 ll_dot.addView(img, params); dots.add(img); } } }
Viewpager无限轮播加小圆点
最新推荐文章于 2018-03-01 14:51:41 发布
本文介绍了一种使用Android平台上的ViewPager组件来实现轮播图的方法,并通过自定义Handler实现了轮播切换的功能。同时,文章还展示了如何添加小圆点指示器以帮助用户了解当前查看的是哪一张图片。
摘要由CSDN通过智能技术生成