Android中ViewPager组件的基本用法及实现图片切换的示例

  package com.example.showviewpager;

  import java.util.ArrayList;

  import android.app.Activity;

  import android.os.Bundle;

  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.ViewGroup;

  import android.widget.ImageView;

  import android.widget.ImageView.ScaleType;

  import android.widget.LinearLayout;

  import android.widget.LinearLayout.LayoutParams;

  public class MainActivity extends Activity {

  private static final int VIEW_NUM = 5;

  private ViewPager viewPager;

  private ImageView[] tagImageViews = new ImageView[VIEW_NUM];

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  addTagImage();

  initViewPager();

  }

  private void addTagImage() {

  LinearLayout layout = (LinearLayout)findViewById(R.id.tagView);

  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(

  LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

  params.setMargins(15, 0, 0, 0);

  for (int i = 0; i < VIEW_NUM; i++) {

  ImageView tagImageView = new ImageView(this);

  tagImageView.setLayoutParams(params);

  tagImageViews[i] = tagImageView;

  if (i == 0) {

  tagImageView.setBackgroundResource(R.drawable.page_current);

  } else {

  tagImageView.setBackgroundResource(R.drawable.page_not_current);

  }

  layout.addView(tagImageView);

  }

  }

  private void initViewPager() {

  viewPager = (ViewPager) findViewById(R.id.viewPager);

  viewPager.setAdapter(new MyAdapter());

  viewPager.setOnPageChangeListener(new OnPageChangeListener() {

  @Override

  public void onPageSelected(int arg0) {

  for (int i = 0; i < tagImageViews.length; i++) {

  if (i == arg0 % VIEW_NUM) {

  tagImageViews[i].setBackgroundResource(R.drawable.page_current);

  } else {

  tagImageViews[i].setBackgroundResource(R.drawable.page_not_current);

  }

  }

  }

  @Override

  public void onPageScrolled(int arg0, float arg1, int arg2) {

  }

  @Override

  public void onPageScrollStateChanged(int arg0) {

  }

  });

  viewPager.setCurrentItem(0);

  }

  class MyAdapter extends PagerAdapter{

  private ArrayList viewList;

  public MyAdapter(){

  viewList = new ArrayList();

  viewList.add(createPagerImageView(R.drawable.pic1));

  viewList.add(createPagerImageView(R.drawable.pic2));

  viewList.add(createPagerImageView(R.drawable.pic3));

  viewList.add(createPagerImageView(R.drawable.pic4));

  viewList.add(createPagerImageView(R.drawable.pic5));

  }

  private View createPagerImageView(int resid){

  LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

  LinearLayout layout = new LinearLayout(MainActivity.this);

  layout.setLayoutParams(params);

  layout.setOrientation(LinearLayout.VERTICAL);

  ImageView imageView = new ImageView(MainActivity.this);

  imageView.setLayoutParams(params);

  imageView.setScaleType(ScaleType.CENTER_CROP);

  imageView.setImageResource(resid);

  layout.addView(imageView);

  return layout;

  }

  @Override

  public int getCount() {

  return Integer.MAX_VALUE;

  }

  @Override

  public boolean isViewFromObject(View arg0, Object arg1) {

  return arg0 == arg1;

  }

  @Override

  public Object instantiateItem(ViewGroup container, int position) {

  ((ViewPager) container).addView(viewList.get(position % VIEW_NUM),0);

  return viewList.get(position % VIEW_NUM);

  }

  @Override

  public void destroyItem(View container, int position, Object object) {

  ((ViewPager) container).removeView(viewList

  .get(position % VIEW_NUM));

  }

  }

  @Override

  public boolean onCreateOptionsMenu(Menu menu) {

  getMenuInflater().inflate(R.menu.main, menu);

  return true;

  }

  @Override

  public boolean onOptionsItemSelected(MenuItem item) {

  int id = item.getItemId();

  if (id == R.id.action_settings) {

  return true;

  }

  return super.onOptionsItemSelected(item);

  }

  }