0
我以爲ViewPager動態顯示圖片,圖片地址數組是靠活動進來的,但是圖片大於3,滑動到左右滑動沒有問題,但是當圖片數小於或等於3,滑塊向右(後退滑動)會出現空白頁,到底是什麼原因?爲什麼我的ViewPager可能是空白頁???
private void initView(){
dateAfterSplit=mScreen.split(",");
tips = new ImageView[dateAfterSplit.length];
for(int i=0; i<tips.length; i++){
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new LayoutParams(10,10));
tips[i] = imageView;
if(i == 0){
tips[i].setBackgroundResource(R.drawable.page_indicator_focused);
}else {
tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
}
mGroup.addView(imageView);
}
mImageViews=new ImageView[dateAfterSplit.length];
for(int i=0; i<mImageViews.length; i++){
ImageView imageView = new ImageView(this);
mImageViews[i] = imageView;
mImageLoader.displayImage(imageView, dateAfterSplit[i],
R.drawable.app_icon, mIsLoaderAlbumFromCache);
}
mViewPager.setAdapter(new MyAdapter());
mViewPager.setOnPageChangeListener(this);
mViewPager.setCurrentItem(mPictureId);}
Left and right infinite loop
public class MyAdapter extends PagerAdapter{
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((ImageView) mImageViews[position % mImageViews.length]);
Log.i("APP", "destroyItem"+position);
}
@Override
public Object instantiateItem(View container, int position) {
try{
if(mImageViews[position % mImageViews.length].getParent()==null){
((ViewPager)container).addView(mImageViews[position % mImageViews.length]);
}
else {
((ViewGroup)mImageViews[position % mImageViews.length].getParent()).removeView(mImageViews[position % mImageViews.length]);
((ViewPager) container).addView(mImageViews[position % mImageViews.length] , 0);
}
} catch (Exception e) {
e.printStackTrace();
}
Log.i("APP", "instantiateItem"+position);
return mImageViews[position % mImageViews.length];
}
}
你能發表一些你的代碼嗎? – Niko 2014-09-02 07:56:48
請發帖 – 2014-09-02 08:03:19
U現在可以看到 – AlexZheng 2014-09-02 08:18:20