2014-09-02 51 views
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]; 
     } 
    } 
+1

你能發表一些你的代碼嗎? – Niko 2014-09-02 07:56:48

+0

請發帖 – 2014-09-02 08:03:19

+0

U現在可以看到 – AlexZheng 2014-09-02 08:18:20

回答

0

嘗試此項添加到PagerAdapter這可以解決您的問題

公衆詮釋getItemPosition(Object對象){回報POSITION_NONE; }

+0

我不認爲這可以解決我的問題,你能再次看到它嗎? – AlexZheng 2014-09-02 08:31:05

相關問題