使用ViewPager首先在layout布局下面添加一个viewpager布局
<android.support.v4.view.ViewPager
android:id="@+id/id_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
第二部在MainActivity中实现
public class MainActivity extends Activity {
// 声明控件ViewPager
private ViewPager mViewPager;
// 声明data,也就是图片资源
private int[] mImageIds = new int[]{ R.drawable.guide_image1,
R.drawable.guide_image2, R.drawable.guide_image3 };
// 定义一个List来存储 ImageView
private List<ImageView> mImages = new ArrayList<ImageView>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
mViewPager.setAdapter(new PagerAdapter() {
//自写方法
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView=new ImageView(MainActivity.this);
//为ImageView设置资源
imageView.setImageResource(mImageIds[position]);
imageView.setScaleType(ScaleType.CENTER_CROP);
container.addView(imageView);
mImages.add(imageView);
return imageView;
}
//自写方法·
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
// TODO Auto-generated method stub
//去除view
container.removeView(mImages.get(position));
}
@Override
public int getCount() {
// TODO Auto-generated method stub
System.out.println("4");
//返回数组的个数
return mImageIds.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
System.out.println("5");
//
return arg0==arg1;
}
});
}
}