2013-09-23 70 views
2

我注意到這個庫的奇怪行爲。當我加載ListView時,我總是會看到圖像顯示在錯誤的地方。當我滾動列表時,它會神奇地去正確的地方。我不確定它爲什麼這樣做。通用圖像加載器:錯誤的位圖被附加到視圖

private final LayoutInflater inflater; 
private final Context mContext; 
protected ImageLoader imageLoader; 
private final DisplayImageOptions options; 
private final Deals deal1; 
private float dpWidth; 

public SingleDealRow(LayoutInflater inflater, Context mContext, Deals deal1, float dpWidth){ 
    this.inflater=inflater; 
    this.mContext=mContext; 
    this.deal1=deal1; 
    this.options = new DisplayImageOptions.Builder() 
    .cacheInMemory(true) 
    .cacheOnDisc(true) 
    .resetViewBeforeLoading(true) 
    .build(); 
    this.imageLoader = ImageLoader.getInstance(); 
    this.dpWidth=dpWidth; 
} 

public View getView(int position, View convertView) { 
    final ViewHolder holder; 
    View view; 
    if (convertView==null){ 
     ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.single_deal_row, null); 
     holder = new ViewHolder((ImageView)viewGroup.findViewById(R.id.thumbnail), 
       (TextView)viewGroup.findViewById(R.id.title), 
       (TextView)viewGroup.findViewById(R.id.price), 
       (TextView)viewGroup.findViewById(R.id.priceSavings), 
       (LinearLayout)viewGroup.findViewById(R.id.rightContainer)); 
     viewGroup.setTag(holder); 
     view = viewGroup; 
    } else{ 
     view=convertView; 
     holder = (ViewHolder)convertView.getTag(); 
    } 

// holder.text1.getLayoutParams().width=(int) (dpWidth-155); 
    int CalculatedWidth=holder.text1.getLayoutParams().width+ holder.thumbnail.getLayoutParams().width+holder.text2.getLayoutParams().width; 
    Log.d("Harnek","actual width is="+(int) (dpWidth)); 
    holder.text1.setText(deal1.title); 
    holder.text2.setText(deal1.price); 
    holder.text3.setText(deal1.priceSavings); 
    holder.thumbnail.setImageBitmap(null); 
    Log.d("Harnek","title="+holder.text1.getLayoutParams().width+" thumbnail="+holder.thumbnail.getLayoutParams().width+" price="+holder.text2.getLayoutParams().width+" priceSavings="+holder.text3.getLayoutParams().width+ 
      " rightContainer="+holder.rightContainer.getLayoutParams().width); 
    /*imageLoader.displayImage(deal1.image, holder.thumbnail, options);  // TODO change this to manually take care of bitmaps 
    imageLoader.displayImage(deal2.image, holder.thumbnail2, options); 
    imageLoader.displayImage(deal3.image, holder.thumbnail3, options);*/ 

    imageLoader.loadImage(deal1.image, options, new SimpleImageLoadingListener() { 
     @Override 
     public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
      // Do whatever you want with Bitmap 
      holder.thumbnail.setImageBitmap(loadedImage); 
     } 
    }); 
    return view; 
} 

public int getViewType() { 
    // TODO Auto-generated method stub 
    return 0; 
} 

public boolean isEnabled() { 
    // TODO Auto-generated method stub 
    return false; 
} 


private static class ViewHolder{ 
    final ImageView thumbnail; 
    final TextView text1; 
    final TextView text2; 
    final TextView text3; 
    final LinearLayout rightContainer; 

    private ViewHolder(ImageView thumbnail,TextView text1,TextView text2,TextView text3,LinearLayout rightContainer){ 
     this.thumbnail=thumbnail; 
     this.text1=text1; 
     this.text2=text2; 
     this.text3=text3; 
     this.rightContainer=rightContainer; 
    } 
    } 

} 

有沒有人有任何想法,我在這裏做錯了什麼?

回答

2

嘗試使用更簡單的代碼:

ImageLoader.getInstance().displayImage(url, holder.thumbnail); 

,並刪除holder.thumbnail.setImageBitmap(null);
希望它會幫助你。

0

如果您使用ListView或GridView,則必須在您的DisplayImageOptions中添加.resetViewBeforeLoading(true)

相關問題