import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
public class MainActivity extends Activity {
// MyViewPagerAdapter adapter;
private ViewPager viewPager;
private List listviews;
private int[] pics = { R.drawable.aa, R.drawable.bb, R.drawable.cc };
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
handler.sendEmptyMessageDelayed(0, 1000);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager_activity);
listviews = new ArrayList();
for (int i = 0; i
ImageView imageView = new ImageView(MainActivity.this);
ViewGroup.LayoutParams viewPagerImageViewParams = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
imageView.setLayoutParams(viewPagerImageViewParams);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(pics[i]);
listviews.add(imageView);
}
viewPager.setAdapter(new ViewPagerAdapter());
viewPager.setCurrentItem(481);
handler.sendEmptyMessageDelayed(0, 1000);// 间隔一秒切换一次
}
class ViewPagerAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, int position) {
if (listviews.get(position % listviews.size()).getParent() != null) {
container
.removeView(listviews.get(position % listviews.size()));
}
container.addView(listviews.get(position % listviews.size()), 0);
return listviews.get(position % listviews.size());
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
}// 这个方法必须要重写,不然会报错
}
}