Universal-Image-Loader:视图附加了错误的位图

时间:2013-09-23 14:23:50

标签: android android-layout universal-image-loader

我注意到这个库的奇怪行为。当我加载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 个答案:

答案 0 :(得分:2)

尝试使用更简单的代码:

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

并删除holder.thumbnail.setImageBitmap(null);希望它能为您提供帮助。

答案 1 :(得分:0)

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