为什么我的ViewPager可能是一个空白页???

时间:2014-09-02 07:55:22

标签: android android-viewpager

我认为ViewPager动态显示图片,图片地址数据按活动进来,但图片大于3,滑动到左右滑动没有问题,但当图片数量小于或等于3时,滑块向右(向后滑动)会有一个空白页面,到底是什么原因?

  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];
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试将此添加到PagerAdapter,这可以解决您的问题

  

public int getItemPosition(Object object){return POSITION_NONE; }