0

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

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

TootsieRockNRoll
  • 3,218
  • 2
  • 25
  • 50

2 Answers2

3

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.

Instead, perhaps try

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

or maybe even setting the ImageView's visibility to INVISIBLE.

aleph_null
  • 5,766
  • 2
  • 24
  • 39
  • That actually would work, I'm using LazyList Library's ImageLoader and I set the default image to something, and now it seems to have worked. Thanks ! – TootsieRockNRoll Jul 24 '14 at 18:56
0

When you create the row in getView, call setTag on the ImageView using the URL string and remove whatever image is currently there. When the image finishes loading, before you set it on the ImageView, call getTag on it and check that the URL still matches.

Karakuri
  • 38,365
  • 12
  • 84
  • 104