Android uses the volley framework to load a large number of images in the ListView

Source: Internet
Author: User

The entries in the 1.listview are to be used with the Networkimageview in volley, but if you use ImageView directly, you can use the tag tag with the URL address set to ImageView in the GetView method

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >    <Com.android.volley.toolbox.NetworkImageViewAndroid:id= "@+id/userimage"Android:layout_width= "100DP"Android:layout_height= "100DP" /></LinearLayout>

2. New picture Cache class Bitmapcache

ImportAndroid.graphics.Bitmap;ImportAndroid.support.v4.util.LruCache;ImportCom.android.volley.toolbox.ImageLoader.ImageCache; Public classBitmapcacheImplementsImagecache {PrivateLrucache<string, bitmap>Mcache;  PublicBitmapcache () {intMaxSize = 4 * 1024 * 1024; Mcache=NewLrucache<string, bitmap>(maxSize) {@Overrideprotected intsizeOf (String key, Bitmap Bitmap) {//return Bitmap.getbytecount ();//>api12 can use it.                returnBitmap.getrowbytes () *bitmap.getheight ();    }        }; } @Override PublicBitmap getbitmap (String url) {returnmcache.get (URL); } @Override Public voidputbitmap (String URL, Bitmap Bitmap) {if(Bitmap! =NULL) {mcache.put (URL, bitmap); }    }}

3.adapater Code

ImportAndroid.content.Context;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportCom.android.volley.RequestQueue;ImportCom.android.volley.toolbox.ImageLoader;ImportCom.android.volley.toolbox.NetworkImageView;ImportCom.android.volley.toolbox.Volley; Public classMyadapterextendsBaseadapter {Privatestring[] list; Privatecontext Context; Privaterequestqueue queue; PrivateImageloader Imageloader;  PublicMyadapter (context context, string[] list) { This. Context =context;  This. List =list; Queue=volley.newrequestqueue (context); Imageloader=NewImageloader (Queue,NewBitmapcache ()); } @Override Public intGetCount () {returnlist.length; } @Override PublicObject GetItem (intposition) {        returnList[position]; } @Override Public LongGetitemid (intposition) {        returnposition; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {Viewholder Holder=NULL; if(Convertview = =NULL) {Holder=NewViewholder (); Convertview= Layoutinflater.from (context). Inflate (R.layout.list_item,NULL); Holder.img=(Networkimageview) Convertview.findviewbyid (r.id.userimage);        Convertview.settag (holder); } Else{Holder=(Viewholder) Convertview.gettag (); }        FinalString Imgurl =List[position]; if(Imgurl! =NULL&&!imgurl.equals ("") {holder.img.setDefaultImageResId (r.drawable.ic_launcher);            Holder.img.setErrorImageResId (R.drawable.ic_launcher);        HOLDER.IMG.SETIMAGEURL (Imgurl, Imageloader); }        returnConvertview; }    classviewholder {networkimageview img; }}

Android uses the volley framework to load a large number of images in the ListView

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.