2012-09-12 66 views
0

所以我有一個android應用程序的功能,應該採取任何數量的圖片,並顯示他們每行5圖片,由於某種原因,當它進入第二行,它只是重複圖片爲第一行,例如,如果我有7個編號爲1-7的圖片,他們將顯示:這裏是我的功能,感謝您的任何建議。Programmatic ImageView android重複行

public void generateImageView(int number, String path){ 
     //ImageView[] imageViewArray = new ImageView[number]; 
     int ROW_ITEMS = 5; // 5 images per row 
     RelativeLayout rl = (RelativeLayout) findViewById(R.id.RelativeLayout1); 
     int limit = number;//limits the number of created imageViews to number 
     int rows = limit/ROW_ITEMS;//number of rows 
     int leftOver = limit % ROW_ITEMS; //see if we have incomplete rows 
     if (leftOver != 0){ 
      rows += 1; 
     } 
     int id = 1000; 
     int belowId = R.id.send; 
     while (rows > 0){ 
      int realItemsPerRow = ROW_ITEMS; 
      if (leftOver != 0 & rows == 1){ 
       realItemsPerRow = Math.min(ROW_ITEMS, leftOver); 
      } 

     for (int i = 0; i < realItemsPerRow; i++){ 

      Bitmap myBitmap = BitmapFactory.decodeFile(path + i + ".png"); 
      ImageView imageViewArray = new ImageView(MainActivity.this); 
      imageViewArray.setId(id); 
      imageViewArray.setImageBitmap(myBitmap); 
      RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
      imageViewArray.setPadding(10,10,0,0); 
      imageViewArray.setAdjustViewBounds(true); 
      imageViewArray.setMaxHeight(80); 
      imageViewArray.setMaxWidth(80); 
      if (i==0) { 
       lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); 
      } else { 
       lp.addRule(RelativeLayout.RIGHT_OF, imageViewArray.getId() -1); 
      } 
      lp.addRule(RelativeLayout.BELOW, belowId); 

      imageViewArray.setLayoutParams(lp); 

      rl.addView(imageViewArray); 
      id++; 
     } 
     belowId = id - 1; 
     rows--; 

回答

1

這是因爲你只是拉動相同的圖像的行中的每個位置:

int j = 0; 

接着上面的改變:

Bitmap myBitmap = BitmapFactory.decodeFile(path + i + ".png"); 

while(rows > 0)加之前

Bitmap myBitmap = BitmapFactory.decodeFile(path + (j * ROW_ITEMS + i) + ".png"); 

a第二下rows--;地址:

j++; 
+0

真棒,完美的作品......然而現在我覺得自己像個白癡...... – user1661396

+0

不要!我已經做了25年以上,而且我還有一週可能會有這種情況。它有助於擁有另一雙眼睛。 – 323go