Today in writing a picture loading class Imageloader, when using the glide to replace my writing imageloader, and then the problem came out!!!
The first question: When using your own written imageloader, in order to prevent the item reuse caused by the picture confusion, set the tag as follows code
@Override PublicView GetView (int position, view Convertview, ViewGroupParent) {Viewholder Holder= NULL;if(Convertview== NULL) {Convertview=Minflater.Inflate (R.Layout.Image_list_item,Parent,false); Holder= NewViewholder (); Holder.ImageView=(ImageView) Convertview.Findviewbyid (R.Id.image); Convertview.Settag (holder); }Else{Holder=(Viewholder) Convertview.Gettag (); } ImageView ImageView=Holder.ImageView; FinalString Tag =(String) ImageView.Gettag (); FinalStringUri=GetItem (position);if(!Uri.equals(Tag)) {ImageView.Setimagedrawable (mdefaultbitmapdrawable); }if(Misgridviewidle&&Mcangetbitmapfromnetwork) {ImageView.Settag (URI); Mimageloader.Bindbitmap (URI, ImageView, Mimagewidth, mimagewidth); }returnConvertview; }
Use Settag (object), where each image's URL address is used as a tag to prevent image confusion
When using glide to replace Imageloader, the code is as follows:
Public View GetView (int position, View Convertview, ViewGroup parent) {Viewholder holder = null;if (Convertview = = null) {Convertview = Minflater. Inflate(R. Layout. Item_gridview, parent, false);Holder = new Viewholder ();Holder. ImageView= (ImageView) convertview. Findviewbyid(R. ID. Image);Convertview. Settag(holder);} else {holder = (Viewholder) convertview. Gettag();} ImageView ImageView = Holder. ImageView;Final String tag = (string) imageView. Gettag();Final String uri = GetItem (position);if (!uri. Equals(tag)) {ImageView. Setimagedrawable(mdefaultbitmapdrawable);} if (Misgridviewidle && mcangetbitmapfromnetwork) {ImageView. Settag(URI);Glide. with(Getapplicationcontext ()). Load(URI). Placeholder(R. drawable. Image_default). Crossfade(). into(ImageView);Mimageloader. Bindbitmap(URI, ImageView, Mimagewidth, Mimagewidth);} return Convertview;}
Operation result Error: Java.lang.IllegalArgumentException:You must not call Settag () on a view Glide is targeting
Then the code of ImageView set tag is removed and the program runs normally.
But this time yo with a problem, that is, the picture is a disorder.
Second question: delete the ImageView tag when the picture is garbled
Workaround: Use the Settag (Int,object) method to set the tag so that the above two issues are resolved
Public View GetView (int position, View Convertview, ViewGroup parent) {Viewholder holder = null;if (Convertview = = null) {Convertview = Minflater. Inflate(R. Layout. Item_gridview, parent, false);Holder = new Viewholder ();Holder. ImageView= (ImageView) convertview. Findviewbyid(R. ID. Image);Convertview. Settag(holder);} else {holder = (Viewholder) convertview. Gettag();} ImageView ImageView = Holder. ImageView;Final String tag = (string) imageView. Gettag(R. ID. Imageloader_uri);Final String uri = GetItem (position);if (!uri. Equals(tag)) {ImageView. Setimagedrawable(mdefaultbitmapdrawable);} if (Misgridviewidle && mcangetbitmapfromnetwork) {ImageView. Settag(R. ID. Imageloader_uri,uri);Glide. with(Getapplicationcontext ()). Load(URI). Placeholder(R. drawable. Image_default). Crossfade(). into(ImageView);Mimageloader. Bindbitmap(URI, ImageView, Mimagewidth, Mimagewidth);} return Convertview;}
The R.id.imageloader_uri here is the new ids.xml under values:
<?xml version="1.0" encoding="utf-8"?><resources> <item name="imageloader_uri" type="id"/></resources>
Picture Loading Library glide--Resolve picture Confusion + Unable to set tag