个人项目需要做到完美的左右可以循环的加载的ViewPager。一开始很快实现了无限循环。但是单个点击页签时有时会出现View为空的情况。分析可知是由于你当前要加载的View被清除的缘故。因为事实上ViewPager只缓存了3个元素这是为了你拖动能看到下一个考虑的。所以我做了改动。不说了搞了几个小时才调好。分享给大家吧:
package com.hanwei.spoof.who; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.View; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.LinearLayout.LayoutParams; import android.widget.TextView; /** * Tab椤甸潰鎵嬪娍婊戝姩鍒囨崲浠ュ強鍔ㄧ敾鏁堟灉 * * @author D.Winter * */ public class SpoofWhoActivity extends Activity { // private ViewPager mPager; private List<View> listViews; private ImageView cursor; private TextView t1, t2, t3; private int offset = 0; private int bmpW; int currIndex; Matrix matrix; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); matrix=new Matrix(); InitImageView(); InitTextView(); InitViewPager(); } /** * 鍒濆鍖栧ご鏍� */ private void InitTextView() { t1 = (TextView) findViewById(R.id.text1); t2 = (TextView) findViewById(R.id.text2); t3 = (TextView) findViewById(R.id.text3); t1.setOnClickListener(new MyOnClickListener(0)); t2.setOnClickListener(new MyOnClickListener(1)); t3.setOnClickListener(new MyOnClickListener(2)); } /** * 鍒濆鍖朧iewPager */ private void InitViewPager() { mPager = (ViewPager) findViewById(R.id.vPager); listViews = new ArrayList<View>(); LayoutInflater mInflater = getLayoutInflater(); listViews.add(mInflater.inflate(R.layout.lay1, null)); listViews.add(mInflater.inflate(R.layout.lay2, null)); listViews.add(mInflater.inflate(R.layout.lay3, null)); mPager.setAdapter(new MyPagerAdapter(listViews)); mPager.setCurrentItem(498); currIndex=498; mPager.setOnPageChangeListener(new MyOnPageChangeListener()); } /** * 鍒濆鍖栧姩鐢� */ private void InitImageView() { cursor = (ImageView) findViewById(R.id.image); bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a) .getWidth(); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int screenW = dm.widthPixels; offset = (screenW / 3 - bmpW) / 2; matrix.postTranslate(offset, cursor.getTop()); cursor.setImageMatrix(matrix); } /** * ViewPager閫傞厤鍣� */ public class MyPagerAdapter extends PagerAdapter { public List<View> mListViews; public MyPagerAdapter(List<View> mListViews) { this.mListViews = mListViews; } @Override public void finishUpdate(View arg0) { } @Override public void notifyDataSetChanged() { super.notifyDataSetChanged(); } @Override public int getCount() { return 1000; } @Override public Object instantiateItem(View arg0, int arg1) { System.out.println("DDDDDD++++"+arg1+"DDDDDDDDDD"+((ViewPager) arg0).getChildCount()); if(((ViewPager) arg0).getChildCount()==3) { ((ViewPager) arg0).removeView(mListViews.get(arg1%3)); } ((ViewPager) arg0).addView(mListViews.get(arg1%3), 0); return mListViews.get(arg1%3); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == (arg1); } @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { } @Override public Parcelable saveState() { return null; } @Override public void startUpdate(View arg0) { } @Override public void destroyItem(View arg0, int arg1, Object arg2) { // TODO Auto-generated method stub } } /** * 澶存爣鐐瑰嚮鐩戝惉 */ public class MyOnClickListener implements View.OnClickListener { private int index = 0; public MyOnClickListener(int i) { index = i; } @Override public void onClick(View v) { int prePos=currIndex-1; int nextPos=currIndex+1; if(prePos%3==index) { mPager.setCurrentItem(prePos); } else if(nextPos%3==index) { mPager.setCurrentItem(nextPos); } else if(currIndex%3==index) { mPager.setCurrentItem(currIndex); } } }; /** * 椤靛崱鍒囨崲鐩戝惉 */ public class MyOnPageChangeListener implements OnPageChangeListener { int one = offset * 2 + bmpW; int two = one * 2; @Override public void onPageSelected(int arg0) { Animation animation = null; currIndex = currIndex%3; switch (arg0%3) { case 0: if (currIndex == 1) { animation = new TranslateAnimation(one, 0, 0, 0); } else if (currIndex == 2) { animation = new TranslateAnimation(two, 0, 0, 0); } break; case 1: if (currIndex == 0) { animation = new TranslateAnimation(offset, one, 0, 0); } else if (currIndex == 2) { animation = new TranslateAnimation(two, one, 0, 0); } break; case 2: if (currIndex == 0) { animation = new TranslateAnimation(offset, two, 0, 0); } else if (currIndex == 1) { animation = new TranslateAnimation(one, two, 0, 0); } break; } currIndex = arg0; if(animation!=null) { animation.setFillAfter(true); animation.setDuration(300); cursor.startAnimation(animation); } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } } }
无限循环的原理是:设置尽量多的元素总数比如1000等等。然后把初始位置设为500或者更多。这样其实是假的无限循环但是用户不会左滑500次吧如果他真的做到了的话好吧他赢了,他可以到我这来领500块钱了哈。还有就是在每次加载View的时候先要清除因为相同的View不能有两个parent。点击无错是根据当前的位置判断的。因为你要知道当前位置和它前面和后面的元素都不是空的所以没有问题的。