加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 通用图像加载程序:错误的位图附加到ImageView

发布时间:2020-12-14 05:33:07 所属栏目:Java 来源:网络整理
导读:我一直在评估 NOSTRA的 Universal-Image-Loader库以异步下载图像并在ListView中显示它们.到目前为止,除了一个问题之外,它的工作正常 有时,当列表被滚动时,内存缓存中的位图会附加到错误的ImageView中.滚动停止后,将附加正确的图像.这种情况是相当罕见的,我找
我一直在评估 NOSTRA的 Universal-Image-Loader库以异步下载图像并在ListView中显示它们.到目前为止,除了一个问题之外,它的工作正常

有时,当列表被滚动时,内存缓存中的位图会附加到错误的ImageView中.滚动停止后,将附加正确的图像.这种情况是相当罕见的,我找不到100%的方式来复制它.我最后一次发生了video.

这是ArticleAdapter代码,可以在那里找到UIL配置和bindView()方法.

public class ArticleAdapter extends CursorAdapter {
    private LayoutInflater inflater;
    private ViewHolder holder;

    public ArticleAdapter(Context context,Cursor cursor,boolean autoRequery) {
        super(context,cursor,autoRequery);
        imageLoader = ImageLoader.getInstance();
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.download_progress_thumb)
                .cacheInMemory()
                .cacheOnDisc()
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
                .build();
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .threadPoolSize(4)
                .discCache(new UnlimitedDiscCache(Utils.getCacheDirectory(context)))
                .defaultDisplayImageOptions(options)
                .build();
        imageLoader.init(configuration);

        titleIndex = cursor.getColumnIndex(Articles.TITLE);
        descriptionIndex = cursor.getColumnIndex(Articles.DESCRIPTION);
        isUnreadIndex = cursor.getColumnIndex(Articles.IS_UNREAD);
        isNewIndex = cursor.getColumnIndex(Articles.IS_NEW);
        urlIndex = cursor.getColumnIndex(Articles.URL);
        hostIndex = cursor.getColumnIndex(Articles.HOST);
        timeIndex = cursor.getColumnIndex(Articles.PUBLISH_TIME);

        bkgUnreadArticle = context.getResources().getColor(R.color.list_bkg_unread_article);
        bkgReadArticle = context.getResources().getColor(R.color.list_bkg_read_article);
        textUnreadTitle = context.getResources().getColor(R.color.list_text_unread_title);
        textReadTitle = context.getResources().getColor(R.color.list_text_read_title);

        inflater = LayoutInflater.from(context);
    }

    @Override
    public void bindView(View view,Context context,Cursor cursor) {
        String date = Utils.format(cursor.getLong(timeIndex),Utils.DATE);
        holder = (ViewHolder) view.getTag();

        holder.titleView.setText(cursor.getString(titleIndex));
        holder.descriptionView.setText(date);

        int isNew = cursor.getInt(isNewIndex);
        if (isNew == 1)
            holder.isNewView.setVisibility(View.VISIBLE);
        else
            holder.isNewView.setVisibility(View.INVISIBLE);

        int isUnread = cursor.getInt(isUnreadIndex);
        if (isUnread == 1){
            holder.titleView.setTextColor(textUnreadTitle);
            holder.rowLayout.setBackgroundColor(bkgUnreadArticle);
        } else {
            holder.titleView.setTextColor(textReadTitle);
            holder.rowLayout.setBackgroundColor(bkgReadArticle);
        }

        String url = cursor.getString(urlIndex);
        String host = cursor.getString(hostIndex);
        if (host.equalsIgnoreCase(Consts.HOST_LENTA) || host.equalsIgnoreCase(Consts.HOST_REALTY)) {
            holder.thumbView.setVisibility(View.VISIBLE);
            imageLoader.displayImage(Utils.makeImageUrl(url,Utils.THUMBNAIL),holder.thumbView);
        } else 
            holder.thumbView.setVisibility(View.GONE);
    }

    @Override
    public View newView(Context context,ViewGroup parent) {
        View v = inflater.inflate(R.layout.articlelist_item,null);
        ViewHolder holder = new ViewHolder();
        holder.titleView = (TextView) v.findViewById(R.id.list_title);
        holder.descriptionView = (TextView) v.findViewById(R.id.list_description);
        holder.thumbView = (ImageView) v.findViewById(R.id.list_thumb);
        holder.isNewView = (TextView) v.findViewById(R.id.list_read_unread);
        holder.rowLayout = (LinearLayout) v.findViewById(R.id.list_row);

        v.setTag(holder);
        return v;
    }
}

我真的很感谢任何关于这件事的帮助.

解决方法

对于ListViews,GridViews和其他列表,其中使用的视图在其适配器中重新使用,您应该在DisplayImageOptions中调用.resetViewBeforeLoading()来防止此效果.

另外文档说:

Init ImageLoader with configuration only once

你只做一次吗?适配器的构造函数不是它的好地方.

UPD:对不起,我的回答没有用. .resetViewBeforeLoading()没有帮助,因为您使用.showStubImage(…).所以你应该有正确的UIL工作,但你没有.这很奇怪

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读