package com.example.viewpagerdemo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; /** * 注:至少需要4张图片才可以正常左右轮换<br> * * ViewPager 图片轮换切换 <br> * 以及<br> * ViewPager 图片通用切换<br>
*/ public class ThreeActivity extends Activity { private List<View> views = new ArrayList<View>(); private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_three); viewPager = (ViewPager) findViewById(R.id.viewPager); // views还没有数据 getViews(); viewPager.setAdapter(new MyViewPagerAdapter()); viewPager.setCurrentItem(views.size() * 100); } private void getViews() { ImageView img1 = new ImageView(this); img1.setImageResource(R.drawable.item01); ImageView img2 = new ImageView(this); img2.setImageResource(R.drawable.item02); ImageView img3 = new ImageView(this); img3.setImageResource(R.drawable.item03); ImageView img4 = new ImageView(this); img4.setImageResource(R.drawable.item04); ImageView img5 = new ImageView(this); img5.setImageResource(R.drawable.item05); ImageView img6 = new ImageView(this); img6.setImageResource(R.drawable.item06); ImageView img7 = new ImageView(this); img7.setImageResource(R.drawable.item07); ImageView img8 = new ImageView(this); img8.setImageResource(R.drawable.item08); views.add(img1); views.add(img2); views.add(img3); views.add(img4); views.add(img5); views.add(img6); views.add(img7); views.add(img8); } // 轮换适配器 private class MyViewPagerAdapter extends PagerAdapter { /* * (non-Javadoc) * * @see android.support.v4.view.PagerAdapter#getCount() */ @Override public int getCount() { return Integer.MAX_VALUE; // TODO 完成PagerAdapter.getCount方法的构建或覆盖 } /* * (non-Javadoc) * * @see * android.support.v4.view.PagerAdapter#isViewFromObject(android.view * .View, java.lang.Object) */ @Override public boolean isViewFromObject(View view, Object object) { return view == object; // TODO 完成PagerAdapter.isViewFromObject方法的构建或覆盖 } /* * (non-Javadoc) * * @see * android.support.v4.view.PagerAdapter#instantiateItem(android.view * .View, int) */ @Override public Object instantiateItem(View container, int position) { ((ViewPager) container).addView( views.get(position % (views.size())), 0); return views.get(position % (views.size())); // TODO 完成MyViewPagerAdapter.instantiateItem方法的构建或覆盖 } /* * (non-Javadoc) * * @see * android.support.v4.view.PagerAdapter#destroyItem(android.view.View, * int, java.lang.Object) */ @Override public void destroyItem(View container, int position, Object object) { ((ViewPager) container).removeView(views.get(position % (views.size()))); // TODO 完成MyViewPagerAdapter.destroyItem方法的构建或覆盖 } } // 通用适配器 private class MyViewPagerAdapter1 extends PagerAdapter { @Override public int getCount() { return views.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(views.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(views.get(position), 0); return views.get(position); } } }