public class MainActivity extends Activity {
private ViewPager vp;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
vp.setCurrentItem(msg.what);
};
};
private RadioGroup rg;
private RadioButton rb1;
private RadioButton rb2;
private RadioButton rb3;
private RadioButton rb4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = (ViewPager) findViewById(R.id.vp);
rg = (RadioGroup) findViewById(R.id.rg);
rb1 = (RadioButton) findViewById(R.id.rb1);
rb1.setChecked(true);
rb2 = (RadioButton) findViewById(R.id.rb2);
rb3 = (RadioButton) findViewById(R.id.rb3);
rb4 = (RadioButton) findViewById(R.id.rb4);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.rb1:
vp.setCurrentItem(0);
break;
case R.id.rb2:
vp.setCurrentItem(1);
break;
case R.id.rb3:
vp.setCurrentItem(2);
break;
case R.id.rb4:
vp.setCurrentItem(3);
break;
default:
break;
}
}
});
// 监听
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
rb1.setChecked(true);
break;
case 1:
rb2.setChecked(true);
break;
case 2:
rb3.setChecked(true);
break;
case 3:
rb4.setChecked(true);
break;
default:
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
System.out.println(arg0 + "----" + arg1 + "-----" + arg2);
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case ViewPager.SCROLL_STATE_DRAGGING:
break;
case ViewPager.SCROLL_STATE_IDLE:
break;
case ViewPager.SCROLL_STATE_SETTLING:
break;
default:
break;
}
}
});
initData();
}
private void initData() {
// TODO Auto-generated method stub
List<View> data = new ArrayList<View>();
data.add(View.inflate(this, R.layout.imgview1, null));
data.add(View.inflate(this, R.layout.ingview2, null));
data.add(View.inflate(this, R.layout.imgview3, null));
data.add(View.inflate(this, R.layout.imgview4, null));
MyPagerAdapter adapter = new MyPagerAdapter(data);
vp.setAdapter(adapter);
// 默认显示哪个
// vp.setCurrentItem(2);
}
// 开始无限播放
public void begin(View view) {
Timer timer = new Timer();
TimerTask task = new TimerTask() {
int index = 0;
@Override
public void run() {
// TODO Auto-generated method stub
index++;
handler.sendEmptyMessage(index);
}
};
timer.schedule(task, 0, 1000);
}
------------------------------------
public class MyPagerAdapter extends PagerAdapter{
private List<View> data;
public MyPagerAdapter(List<View> data) {
super();
this.data = data;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
//return data.size();
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
//View view = data.get(position%data.size());
// container.removeView(view);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
//对ViewPager页号求模取出View列表中要显示的项
position %= data.size();
if (position<0){
position = data.size()+position;
}
View view = data.get(position);
//如果View已经在之前添加到了一个父组件,则必须先remove,否则会抛出IllegalStateException。
ViewParent vp =view.getParent();
if (vp!=null){
ViewGroup parent = (ViewGroup)vp;
parent.removeView(view);
}
container.addView(view);
//add listeners here if necessary
return view;
}
}