首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Universal-Image-Loader:错误的位图附加到视图

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

Stack Overflow用户
提问于 2013-09-23 22:23:50
回答 2查看 820关注 0票数 2

我注意到这个库有奇怪的行为。当我加载ListView时,我总是会看到图像显示在错误的位置。当我滚动列表时,它会神奇地转到正确的位置。我不确定它为什么要这么做。

代码语言:javascript
复制
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;
    }
  }

}

有没有人知道我做错了什么?

EN

回答 2

Stack Overflow用户

发布于 2013-09-23 22:32:43

尝试使用更简单的代码:

代码语言:javascript
复制
ImageLoader.getInstance().displayImage(url, holder.thumbnail);

并删除holder.thumbnail.setImageBitmap(null);

希望能对你有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2015-06-16 13:58:41

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18961783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档