Volley+networkimageview the left icon in the list item that implements the list interface displays the "Do not reload when the icon for the list item that has been loaded is flipped over"

Source: Internet
Author: User

Reference: http://blog.csdn.net/guolin_blog/article/details/17482165

The adapter for the list I'm using is inherited arrayadapter, so about using Volley+networkimageview to get the icon resources is performed in GetView.

/**List of adapters: Inherit Arrayadapter*/ Public classNewslistarrayadapterextendsArrayadapter<cnblogsnews>{        Private Static FinalString TAG = "Newslistarrayadapter"; Private intListitemresourceid; PrivateContext Thiscontext; PrivateLrucache<string, bitmap> Mmemorycache;//must be set to a global variable instead of written in the Bitmapcache class, and the instantiation must also be written in the constructor         PublicNewslistarrayadapter (Context context,intResource, List<CnBlogsNews>objects) {        Super(Context, resource, objects); Listitemresourceid=resource; Thiscontext=context; Initlrucache ();//improved Bitmapcache constructor    }        /*improved Bitmapcache constructor*/    Private voidInitlrucache () {//gets the maximum amount of available memory, using memory exceeding this value can cause a OutOfMemory exception        intMaxMemory = (int) (Runtime.getruntime (). MaxMemory ()/1024);//in bytes, divided by 1024 is the KB unitLOG.V (TAG, "maxmemory=" +maxmemory+ "KB");//196608KB = = 192M                intMaxSize = 1 * 1024 * 1024; //LRUCache Incoming cache values by constructor, in kilobytesMmemorycache=NewLrucache<string, bitmap>(maxSize) {@Overrideprotected intsizeOf (String key, Bitmap Bitmap) {//Override this method to measure the size of each picture, returning the number of pictures by default. LOG.V (TAG, "bitmap.getrowbytes () * bitmap.getheight () =" +bitmap.getrowbytes () *bitmap.getheight ()); returnBitmap.getrowbytes () *bitmap.getheight ();    }        }; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {                FinalCnblogsnews cnblogsnews =GetItem (position); View View=NULL;                Viewholder Holder; if(Convertview = =NULL) {View= Layoutinflater.from (Thiscontext). Inflate (Listitemresourceid, parent,false); Holder=NewViewholder (); Holder.newslist_topicicon=(Networkimageview) View.findviewbyid (R.id.newslist_topicicon); Holder.newslist_title=(TextView) View.findviewbyid (r.id.newslist_title); Holder.newslist_published=(TextView) View.findviewbyid (r.id.newslist_published);                    View.settag (holder); }Else{View=Convertview; Holder=(Viewholder) View.gettag (); }                //Load icon ResourceLoadimageviewwithurl (Holder.newslist_topicicon,cnblogsnews.getnewstopiciconurl ());        Holder.newslist_title.setText (Cnblogsnews.getnewstitle ()); Holder.newslist_Published.setText (cnblogsnews.getnewspublished ()+ ": Serial number =" +position); //Add a link address, open by WebView wayView.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {webviewactivity.openwebviewactivity (Thiscontext, Cnblogsnews.getnewslink ());                }        }); returnview; }            classviewholder{Networkimageview Newslist_topicicon;//News Theme PicturesTextView Newslist_title;//News HeadlinesTextView newslist_published;//Press release Time    }            //load pictures based on URL address        /** 1. Create a Requestqueue object. * 2.     Creates a Imageloader object. * 3.     Gets a Imagelistener object. * 4.     Call Imageloader's Get () method to load a picture on the network. * */    Private voidLoadimageviewwithurl (Networkimageview imgview, String urlstr) {requestqueue requestqueue=Volley.newrequestqueue (Thiscontext); //Implementing caching CapabilitiesImageloader Imgloader =NewImageloader (Requestqueue,NewBitmapcache ()); //Imagelistener Imglistener = Imageloader.getimagelistener (Imgview, R.drawable.ic_empty, R.drawable.ic_error); //Imgloader.get (Urlstr, imglistener);Imgview.setdefaultimageresid (R.drawable.ic_empty);                Imgview.seterrorimageresid (R.drawable.ic_error);            Imgview.setimageurl (Urlstr, Imgloader); }        //set the size of the cached picture to 10m== improved    Private classBitmapcacheImplementsImagecache {@Override PublicBitmap getbitmap (String url) {log.v (TAG,"getbitmap==url==" +URL); returnmmemorycache.get (URL); } @Override Public voidputbitmap (String URL, Bitmap Bitmap) {log.v (TAG,"putbitmap==url==" +URL); if(mmemorycache.get (URL) = =NULL) {mmemorycache.put (URL, bitmap); }                    }    }}
Newslistarrayadapter

The point is that instead of instantiating the LRUCache class in the Bitmapcache class, the LRUCache class is assigned a global variable, guaranteeing that the icon for all items in the entire list is using the same lrucache.

If you instantiate LRUCache in the Bitmapcache Class (code below), you will see the icon reload when you page down and then back to the previous one.

 Public classBitmapcacheImplementsImagecache {PrivateLrucache<string, bitmap>Mcache;  PublicBitmapcache () {intMaxSize = 10 * 1024 * 1024; Mcache=NewLrucache<string, bitmap>(maxSize) {@Overrideprotected intsizeOf (String key, Bitmap Bitmap) {returnBitmap.getrowbytes () *bitmap.getheight ();    }        }; } @Override PublicBitmap getbitmap (String url) {returnmcache.get (URL); } @Override Public voidputbitmap (String URL, Bitmap Bitmap) {mcache.put (URL, Bitmap); }}
Bitmapcache

Volley+networkimageview the left icon in the list item that implements the list interface displays the "Do not reload when the icon for the list item that has been loaded is flipped over"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.