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

前端之家收集整理的这篇文章主要介绍了java – 通用图像加载程序:错误的位图附加到ImageView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在评估 NOSTRAUniversal-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工作,但你没有.这很奇怪

原文链接:https://www.f2er.com/java/122198.html

猜你在找的Java相关文章