简体   繁体   English

带有远程ImageView的Android ViewHolder

[英]Android ViewHolder with remote ImageView

I Have implemented ViewHolder pattern im my adapter , where I have an ImageView populated with a remote image, the problem is when I scroll I always get a wrong image before having the correct one (after a fiew ms), I guess it is due to the fact that the current view IS a previously inflated view, but I don't see any possible solutions to this 我已经在我的adapter实现了ViewHolder模式,在其中我有一个装有远程图像的ImageView ,问题是当我滚动时,总是在获得正确的图像之前(错误的ms后)得到了错误的图像,我想这是由于当前视图是以前夸大的视图,但我看不到任何可能的解决方案

if (rowView == null) {
       rowView  =   mInflater.inflate(R.layout.layout, parent, false);
       ViewHolder viewHolder                =   new ViewHolder();
       viewHolder.imageView     =   (ImageView)  rowView.findViewById(R.id.image);

       rowView.setTag(viewHolder);
}

ViewHolder viewHolder = (ViewHolder) rowView.getTag();

viewHolder.imageView.setImageResource(0);
imageLoader.DisplayImage(URL, viewHolder.imageView);

Thank you 谢谢

You seem to be on the right track with your code. 您的代码似乎走在正确的轨道上。 The only issue is that imageView.setImageResource(0) doesn't seem to clear the image, at least on my Nexus 5 running kitkat. 唯一的问题是imageView.setImageResource(0)似乎无法清除图像,至少在我运行Nexus 5的kitkat上。

Instead, perhaps try 相反,也许尝试

imageView.setImageDrawable(new ColorDrawable(android.R.color.transparent));

or maybe even setting the ImageView's visibility to INVISIBLE . 甚至将ImageView's可见性设置为INVISIBLE

When you create the row in getView , call setTag on the ImageView using the URL string and remove whatever image is currently there. getView创建行时,使用URL字符串在ImageView上调用setTag并删除当前存在的任何图像。 When the image finishes loading, before you set it on the ImageView , call getTag on it and check that the URL still matches. 图像加载完成后,请在ImageView上进行设置,然后在其上调用getTag并检查URL是否仍然匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM