package com.lixu.pagerview; import java.util.ArrayList; import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView; public class MainActivity extends Activity {
ViewPager mViewPager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.vp);
PagerAdapter mPagerAdapter = new myAdapter(this);
mViewPager.setAdapter(mPagerAdapter);//设置适配器
} public class myAdapter extends PagerAdapter {
private ArrayList<ImageView> views;
private Context context; @Override
public int getCount() {
return views.size();
} public myAdapter(Context context) {
views = new ArrayList<ImageView>();
this.context = context;
int[] image = new int[] { R.drawable.a1, R.drawable.a10, R.drawable.a2, R.drawable.aa8, R.drawable.aa9,//设置图片id
R.drawable.f14 };
for (int i = ; i < ; i++) {
ImageView mImageView = new ImageView(context);
mImageView.setImageResource(image[i]);
views.add(mImageView); } } @Override
public View instantiateItem(ViewGroup container, int position) {
container.addView(views.get(position));
return views.get(position);
} @Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(views.get(position));
} @Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
} }
}