图片轮播加小圆点

时间:2021-03-19 20:44:15
<span style="font-size:18px;">public class F1 extends Fragment {

	private ViewPager vp;
	private ImageView im1;
	private ImageView im2;
	private ImageView im3;
	private ImageView im4;
	private ImageView im5;
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			int currentItem = vp.getCurrentItem();
			int i = currentItem + 1;
			vp.setCurrentItem(i);
			handler.sendEmptyMessageDelayed(8, 2000);
		}

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.f1, null);

		vp = (ViewPager) v.findViewById(R.id.viewPager);
                获取控件小圆点
               im1 = (ImageView) v.findViewById(R.id.im1);
		im2 = (ImageView) v.findViewById(R.id.im2);
		im3 = (ImageView) v.findViewById(R.id.im3);
		im4 = (ImageView) v.findViewById(R.id.im4);
		im5 = (ImageView) v.findViewById(R.id.im5);
		gv = (GridView) v.findViewById(R.id.gridView1);
		getdata();
		new Thread() {

			@Override
			public void run() {
				sendGet();
			}

		}.start();
		im1.setImageResource(R.drawable.point_selected);

		vp.setCurrentItem(Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2
				% integer_list.size());
		handler.sendEmptyMessageDelayed(9, 2000);
		vp.setAdapter(pa);
		vp.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			 public void onPageSelected(int arg0) {
				for (ImageView iv : imglist) {
					iv.setImageResource(R.drawable.point_mormal);
				}
				imglist.get(arg0 % integer_list.size()).setImageResource(
						R.drawable.point_selected);

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
		return v;
	}

	private PagerAdapter pa = new PagerAdapter() {

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return Integer.MAX_VALUE;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View view = viewlist.get(position % viewlist.size());
			container.addView(view);
			return view;
		}
	};

	private ArrayList<Integer> integer_list;
	private ArrayList<ImageView> imglist;

	public void getdata() {
		integer_list = new ArrayList<Integer>();
                添加轮播图片
		integer_list.add(R.drawable.a);
		integer_list.add(R.drawable.b);
		integer_list.add(R.drawable.d);
		integer_list.add(R.drawable.f);
		integer_list.add(R.drawable.g);

		for (int i : integer_list) {
			View inflate = View.inflate(getActivity(), R.layout.a1, null);
			inflate.setBackgroundResource(i);
			viewlist.add(inflate);
		}
		imglist = new ArrayList<ImageView>();
		imglist.add(im1);
		imglist.add(im2);
		imglist.add(im3);
		imglist.add(im4);
		imglist.add(im5);
	}


}</span>