UI高级_轮播图

MainActivity

package com.example.day_3;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends Activity {

	private ViewPager viewPager;
	private RadioGroup radioGroup;
	ArrayList list = new ArrayList();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		viewPager = (ViewPager) findViewById(R.id.v);
		radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
		int arr [] = {R.drawable.s1,R.drawable.s2,R.drawable.s3,R.drawable.s4};
		for (int i = 0; i < arr.length; i++) {
			ImageView imageView = new ImageView(MainActivity.this);
			//图片下标
			imageView.setImageResource(arr[i]);
			list.add(imageView);
			//获得小圆点 
			RadioButton radioButton = new RadioButton(MainActivity.this);
			radioGroup.addView(radioButton);
		}
		//适配器
		viewPager.setAdapter(new Adapter(list));
		//选中第一个
		radioGroup.check(radioGroup.getChildAt(0).getId());
		//小圆点跟着走
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {
				arg0 = arg0 % list.size();
				radioGroup.check(radioGroup.getChildAt(arg0).getId());
			}
			@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
			}
		});
		//无限轮播
		handler.sendEmptyMessageDelayed(0, 1000);
	}

	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
			int i =	viewPager.getCurrentItem()+1;
			viewPager.setCurrentItem(i);
			handler.sendEmptyMessageDelayed(0, 1000);
			break;

			default:
				break;
			}
		};
	};

}

MyPagerAdapter

package com.example.day_3;

import java.util.ArrayList;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class Adapter extends PagerAdapter {
	ArrayList list = new ArrayList();

	public Adapter(ArrayList list) {
		this.list = list;
	}

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

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0==arg1;
	}
	//调用初始条目
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		position = position %list.size();
		ImageView imageView = list.get(position);
		//添加到viewpage
		container.addView(imageView);
		return imageView;
	}
	//销毁
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}
}

 

你可能感兴趣的:(UI_高级)