2014-05-09 28 views
0

我正在解析vk的羣組牆。有些帖子有照片(從1到10),有些沒有。我需要展示它們。但是,所有的圖像都會重複。我無法讓它們正確顯示。目前我正在使用畢加索。它說它可以處理ListView圖像,但它不會。我在谷歌上的其他方式也不起作用。問題是如何正確處理圖像。這是一段負責照片的代碼。重複ListView中的圖像

private void place_photos(View view, ArrayList<VKPhoto> photos) { 
    int pcount = photos.size(); 
    final View v = view; 
    TableRow first = (TableRow)v.findViewById(R.id.first_row); 
    TableRow second = (TableRow)v.findViewById(R.id.second_row); 
    TableRow third = (TableRow)v.findViewById(R.id.third_row); 

    for (int i = 0; i < pcount; i++) { 
     if (i == 1) { 
      ImageView img = new ImageView(mContext); 
      String url = photos.get(i).photo_807; 
      img.setTag(url); 
      Picasso.with(mContext).load(url).into(img); 
      if (first != null) 
       first.addView(img); 
     } 
     if (i == 2 || i == 3) { 
      ImageView img = new ImageView(mContext); 
      String url = photos.get(i).photo_807; 
      img.setTag(url); 
      Picasso.with(mContext).load(url).into(img); 
      if (second != null) 
       second.addView(img); 
     } 
     if (i > 3) { 
      ImageView img = new ImageView(mContext); 
      String url = photos.get(i).photo_807; 
      img.setTag(url); 
      Picasso.with(mContext).load(url).into(img); 
      if (third != null) 
       third.addView(img); 
     } 
    } 
} 

這就是它是如何調用getView方法:

if (post.attachments.hasPhoto) { 
     place_photos(v, post.attachments.photos); 
} else { 
     viewHolder.photo_wrapper.setVisibility(View.GONE); 
} 

UPD 以下是完整的適配器代碼:https://gist.github.com/alexbat98/7f22598e7e73d301a4ef

+0

如果你使用自定義的適配器粘貼這裏的代碼 –

+0

@ Kat-hat上傳到Gist –

+0

我不知道,但你設置viewholder.photo? – Cocorico

回答

0

嘗試

ImageView img; 

ViewHolder類中和th en使用它作爲

viewHolder.img = viewHolder.photo; 
    viewHolder.img.setTag(photo_100); 
    imageLoader.DisplayImage(photo_100, viewHolder.img); 
+0

其實viewHolder.photo是一個用戶頭像。我沒有問題。問題在於place_photos方法。這是一個屏幕截圖:https://db.tt/9PvaoRPS –

+0

Sry,但代碼實際上看起來有些雜亂......你可以做的是,你找到的適配器中的imageview正在重複,把它放到ViewHolder類中,並使用和我上面的「img」一樣(ImageView) –

+0

是的,代碼有點混亂。但我怎樣才能使用ViewHolder?我以無限的方式創造了這些觀點。所以,我沒有在我的XML佈局中擁有它們,我不知道我會有多少照片。 –

0

問題應歸因於ListView回收。

儘量覆蓋getViewTypeCountgetItemViewType

如果您的視圖類型不同,則返回類型計數,例如如果3種不同的類型,那麼

@Override 
public int getViewTypeCount() { 
    return 3; 
} 

並且,然後告訴ListView哪個項目的位置是從其他,例如不同的,如果位置0和1是不同於其他項目然後改變它這樣

@Override 
public int getItemViewType(int position){ 
    // return a unique number 
    if(position == 0){ 
     return 0; 
    } 
    if(position == 1){ 
     return 1; 
    } 
    else { 
     return 2; 
    } 
}