android - ImageView inside Gridview repeating image when scrolling (using ImageDownloader) -
as title says, have problem.
because need retrieve images network, have implemented solution explained here
i post adapter , imagedownloader code (i have taken liberty modify downloader in order make functionality more appropriate program).
adapter method:
@override public view getview(int position, view view, viewgroup parent) { viewholder holder; if (view == null) { view = inflater.inflate(r.layout.grid_adapter_camera_layout, parent, false); holder = new viewholder(); holder.progress = (progressbar) view.findviewbyid(r.id.progressbar); holder.image = (imageview) view.findviewbyid(r.id.camera_image); holder.text = (textview) view.findviewbyid(r.id.camera_name); holder.text.setellipsize(textutils.truncateat.end); holder.text.setlines(2); view.settag(holder); } else { holder = (viewholder) view.gettag(); } imgdownloader.download(values.get(position).geturl(), holder.image, holder.progress); }
and here imagedownloader (i post proper part of code, if need further don't hesitate ask it).
public void download(string url, imageview imageview, progressbar progress) { bitmap bitmap = getbitmapfromcache(url); if (bitmap != null) { cancelpotentialdownload(url, imageview); imageview.setimagebitmap(bitmap); } else if(cancelpotentialdownload(url, imageview)){ bitmapdownloadertask task = new bitmapdownloadertask(imageview, progress); imageview.settag(new weakreference<bitmapdownloadertask>(task)); task.execute(url); } } private boolean cancelpotentialdownload(string url, imageview imageview) { bitmapdownloadertask bitmapdownloadertask = getbitmapdownloadertask(imageview); if (bitmapdownloadertask != null) { string bitmapurl = bitmapdownloadertask.url; if ((bitmapurl == null) || (!bitmapurl.equals(url))) { bitmapdownloadertask.cancel(true); } else { // same url being downloaded. return false; } } return true; } @suppresswarnings("unchecked") private bitmapdownloadertask getbitmapdownloadertask(imageview imageview) { if (imageview != null && imageview.gettag() != null) { return ((weakreference<bitmapdownloadertask>) imageview.gettag()).get(); } return null; } public class bitmapdownloadertask extends asynctask<string, void, bitmap> { string url; weakreference<imageview> viewreference; weakreference<progressbar> progressreference; public bitmapdownloadertask(imageview view, progressbar progress) { view.settag(false); viewreference = new weakreference<imageview>(view); progressreference = new weakreference<progressbar>(progress); } @override protected bitmap doinbackground(string... params) { url=params[0]; log.e("download url", url); return downloadbitmap(params[0]); } @override protected void onpostexecute(bitmap result) { if (iscancelled()) result = null; addbitmaptocache(url, result); if (viewreference != null) { imageview imgview = viewreference.get(); bitmapdownloadertask bitmapdownloadertask = getbitmapdownloadertask(imgview); if (this == bitmapdownloadertask) { if (result != null) imgview.setimagebitmap(result); else imgview.setimageresource(r.drawable.no_signal); } } if (progressreference != null) { progressbar prog = progressreference.get(); if (prog != null) prog.setvisibility(view.gone); } } }
i suggest u use library image downloading, picasso great job it. thing take @ this video dev-bytes. animations trick should same downloading images. 2 steps should work, i've done few times , worked perfcetly.
Comments
Post a Comment