MainActivity is as follows:
Package cc. testlistview; import java. util. arrayList; import com. example. testlistview. r; import com. nostra13.universalimageloader. cache. disc. naming. md5FileNameGenerator; import com. nostra13.universalimageloader. core. imageLoader; import com. nostra13.universalimageloader. core. imageLoaderConfiguration; import com. nostra13.universalimageloader. core. assist. queueProcessingType; import android. OS. bundle; import Droid. app. activity; import android. content. context; import android. widget. listView;/*** Demo Description: * use the third-party tool universal-image-loader to asynchronously load images for ListView ** reference: * 1 http://blog.csdn.net/hkg1pek/article/details/9057815 * 2 http://blog.csdn.net/wwj_748/article/details/10079311 * Thank you very much **/public class MainActivity extends Activity {private ListView mListView; private ImageLoader mImageLoader; Private Context mContext; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); initView (); initImageLoader (this);} public void initView () {mContext = this; mImageLoader = ImageLoader. getInstance (); mListView = (ListView) findViewById (R. id. listView); ArrayList <ListViewItem> arrayList = new ArrayList <ListViewItem> (); String imagesUrl [] = ImagesUrl. urls; ListViewItem listViewItem = null; for (int I = 0; I <imagesUrl. length; I ++) {listViewItem = new ListViewItem ("This is" + I, imagesUrl [I]); arrayList. add (listViewItem);} ListViewAdapter adapter = new ListViewAdapter (arrayList, mContext, mImageLoader); mListView. setAdapter (adapter);}/*** Configuration of ImageLoader: * This configuration tuning is M. * You can tune every optio N, you may tune some of them, * or you can create default configuration by * ImageLoaderConfiguration. createDefault (this) method. ** Note: * 1 enableLogging () // Not necessary in common * 2 in actual projects, you can put it in Application */public static void initImageLoader (Context context) {ImageLoaderConfiguration config = new ImageLoaderConfiguration. builder (context ). threadPriority (Thread. NORM_PRIORITY-2 ). denyCach EImageMultipleSizesInMemory (). discCacheFileNameGenerator (new Md5FileNameGenerator ()). tasksProcessingOrder (QueueProcessingType. LIFO ). enableLogging (). build (); ImageLoader. getInstance (). init (config) ;}@ Overrideprotected void onDestroy () {super. onDestroy (); if (mImageLoader! = Null) {mImageLoader. clearMemoryCache (); mImageLoader. clearDiscCache ();}}}
The ListViewAdapter is as follows:
Package cc. testlistview; import java. util. arrayList; import java. util. collections; import java. util. using list; import java. util. list; import android. content. context; import android. graphics. bitmap; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. textView; import com. example. tes Tlistview. r; import com. nostra13.universalimageloader. core. displayImageOptions; import com. nostra13.universalimageloader. core. imageLoader; import com. nostra13.universalimageloader. core. assist. simpleImageLoadingListener; import com. nostra13.universalimageloader. core. display. fadeInBitmapDisplayer; public class ListViewAdapter extends BaseAdapter {private ArrayList <ListViewItem> mArrayList; private Contex T mContext; private ImageLoader mImageLoader; private DisplayImageOptions mDisplayImageOptions; private ImageLoadingListenerImpl inline; public ListViewAdapter (ArrayList <ListViewItem> arrayList, Context context, ImageLoader imageLoader) {super (); this. mArrayList = arrayList; this. mContext = context; this. mImageLoader = imageLoader; int defaultImageId = R. drawable. ic_launcher; mDisplayI MageOptions = new DisplayImageOptions. builder (). showStubImage (defaultImageId ). showImageForEmptyUri (defaultImageId ). showImageOnFail (defaultImageId ). cacheInMemory (true ). cacheOnDisc (true ). resetViewBeforeLoading (). build (); mImageLoadingListenerImpl = new ImageLoadingListenerImpl ();} public int getCount () {if (mArrayList = null) {return 0;} else {return mArrayList. size () ;}} public Object getItem (int p Osition) {if (mArrayList = null) {return null;} else {return mArrayList. get (position) ;}} public long getItemId (int position) {return position;} public View getView (int position, View convertView, ViewGroup parent) {ViewHolder holder = null; if (convertView = null) {holder = new ViewHolder (); convertView = LayoutInflater. from (this. mContext ). inflate (R. layout. listviewitem, null, false); holder. textView = (TextView) ConvertView. findViewById (R. id. textView); holder. imageView = (ImageView) convertView. findViewById (R. id. imageview); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag ();} if (this. mArrayList! = Null) {ListViewItem listViewItem = this. mArrayList. get (position); if (holder. textView! = Null) {holder. textView. setText (listViewItem. getContent ();} if (holder. imageView! = Null) {try {// load the image mImageLoader. displayImage (listViewItem. imageURL, holder. imageView, mDisplayImageOptions, mImageLoadingListenerImpl);} catch (Exception e) {e. printStackTrace () ;}}return convertView;} // listens to images asynchronously loading public static class ImageLoadingListenerImpl extends SimpleImageLoadingListener {public static final List <String> displayedImages = Collections. synchronizedList (new synchronized list <String> (); @ Ov Erridepublic void onLoadingComplete (String imageUri, View view, Bitmap bitmap) {if (bitmap! = Null) {ImageView imageView = (ImageView) view; boolean isFirstDisplay =! DisplayedImages. contains (imageUri); if (isFirstDisplay) {// fade-in effect of the image FadeInBitmapDisplayer. animate (imageView, 500); displayedImages. add (imageUri); System. out. println ("==> loading" + imageUri) ;}}} private class ViewHolder {ImageView imageView; TextView textView ;}}
ListViewItem is as follows:
package cc.testlistview;public class ListViewItem {String content;String imageURL;public ListViewItem(String content, String imageURL) {super();this.content = content;this.imageURL = imageURL;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public String getImageURL() {return imageURL;}public void setImageURL(String imageURL) {this.imageURL = imageURL;}}
ImagesUrl:
package cc.testlistview;public class ImagesUrl {public final static String[] Urls = new String[] {"https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s160-c/A%252520Photographer.jpg","https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s160-c/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg","https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s160-c/Another%252520Rockaway%252520Sunset.jpg","https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s160-c/Antelope%252520Butte.jpg","https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s160-c/Antelope%252520Hallway.jpg","https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s160-c/Antelope%252520Walls.jpg","https://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s160-c/Apre%2525CC%252580s%252520la%252520Pluie.jpg","https://lh3.googleusercontent.com/-s-AFpvgSeew/URquc6dF-JI/AAAAAAAAAbs/Mt3xNGRUd68/s160-c/Backlit%252520Cloud.jpg","https://lh5.googleusercontent.com/-bvmif9a9YOQ/URquea3heHI/AAAAAAAAAbs/rcr6wyeQtAo/s160-c/Bee%252520and%252520Flower.jpg","https://lh5.googleusercontent.com/-n7mdm7I7FGs/URqueT_BT-I/AAAAAAAAAbs/9MYmXlmpSAo/s160-c/Bonzai%252520Rock%252520Sunset.jpg","https://lh6.googleusercontent.com/-4CN4X4t0M1k/URqufPozWzI/AAAAAAAAAbs/8wK41lg1KPs/s160-c/Caterpillar.jpg","https://lh3.googleusercontent.com/-rrFnVC8xQEg/URqufdrLBaI/AAAAAAAAAbs/s69WYy_fl1E/s160-c/Chess.jpg","https://lh5.googleusercontent.com/-WVpRptWH8Yw/URqugh-QmDI/AAAAAAAAAbs/E-MgBgtlUWU/s160-c/Chihuly.jpg","https://lh5.googleusercontent.com/-0BDXkYmckbo/URquhKFW84I/AAAAAAAAAbs/ogQtHCTk2JQ/s160-c/Closed%252520Door.jpg","https://lh3.googleusercontent.com/-PyggXXZRykM/URquh-kVvoI/AAAAAAAAAbs/hFtDwhtrHHQ/s160-c/Colorado%252520River%252520Sunset.jpg","https://lh6.googleusercontent.com/-9lzOk_OWZH0/URquoo4xYoI/AAAAAAAAAbs/AwgzHtNVCwU/s160-c/Frantic.jpg","https://lh3.googleusercontent.com/-0X3JNaKaz48/URqupH78wpI/AAAAAAAAAbs/lHXxu_zbH8s/s160-c/Golden%252520Gate%252520Afternoon.jpg","https://lh6.googleusercontent.com/-95sb5ag7ABc/URqupl95RDI/AAAAAAAAAbs/g73R20iVTRA/s160-c/Golden%252520Gate%252520Fog.jpg","https://lh3.googleusercontent.com/-JB9v6rtgHhk/URqup21F-zI/AAAAAAAAAbs/64Fb8qMZWXk/s160-c/Golden%252520Grass.jpg","https://lh4.googleusercontent.com/-EIBGfnuLtII/URquqVHwaRI/AAAAAAAAAbs/FA4McV2u8VE/s160-c/Grand%252520Teton.jpg","https://lh4.googleusercontent.com/-WoMxZvmN9nY/URquq1v2AoI/AAAAAAAAAbs/grj5uMhL6NA/s160-c/Grass%252520Closeup.jpg","https://lh3.googleusercontent.com/-6hZiEHXx64Q/URqurxvNdqI/AAAAAAAAAbs/kWMXM3o5OVI/s160-c/Green%252520Grass.jpg","https://lh5.googleusercontent.com/-6LVb9OXtQ60/URquteBFuKI/AAAAAAAAAbs/4F4kRgecwFs/s160-c/Hanging%252520Leaf.jpg","https://lh4.googleusercontent.com/-zAvf__52ONk/URqutT_IuxI/AAAAAAAAAbs/D_bcuc0thoU/s160-c/Highway%2525201.jpg","https://lh6.googleusercontent.com/-H4SrUg615rA/URquuL27fXI/AAAAAAAAAbs/4aEqJfiMsOU/s160-c/Horseshoe%252520Bend%252520Sunset.jpg","https://lh4.googleusercontent.com/-JhFi4fb_Pqw/URquuX-QXbI/AAAAAAAAAbs/IXpYUxuweYM/s160-c/Horseshoe%252520Bend.jpg","https://lh5.googleusercontent.com/-UGgssvFRJ7g/URquueyJzGI/AAAAAAAAAbs/yYIBlLT0toM/s160-c/Into%252520the%252520Blue.jpg","https://lh3.googleusercontent.com/-CH7KoupI7uI/URquu0FF__I/AAAAAAAAAbs/R7GDmI7v_G0/s160-c/Jelly%252520Fish%2525202.jpg","https://lh4.googleusercontent.com/-pwuuw6yhg8U/URquvPxR3FI/AAAAAAAAAbs/VNGk6f-tsGE/s160-c/Jelly%252520Fish%2525203.jpg","https://lh5.googleusercontent.com/-GoUQVw1fnFw/URquv6xbC0I/AAAAAAAAAbs/zEUVTQQ43Zc/s160-c/Kauai.jpg","https://lh6.googleusercontent.com/-8QdYYQEpYjw/URquwvdh88I/AAAAAAAAAbs/cktDy-ysfHo/s160-c/Kyoto%252520Sunset.jpg","https://lh4.googleusercontent.com/-vPeekyDjOE0/URquwzJ28qI/AAAAAAAAAbs/qxcyXULsZrg/s160-c/Lake%252520Tahoe%252520Colors.jpg","https://lh4.googleusercontent.com/-xBPxWpD4yxU/URquxWHk8AI/AAAAAAAAAbs/ARDPeDYPiMY/s160-c/Lava%252520from%252520the%252520Sky.jpg","https://lh3.googleusercontent.com/-897VXrJB6RE/URquxxxd-5I/AAAAAAAAAbs/j-Cz4T4YvIw/s160-c/Leica%25252050mm%252520Summilux.jpg","https://lh5.googleusercontent.com/-qSJ4D4iXzGo/URquyDWiJ1I/AAAAAAAAAbs/k2pBXeWehOA/s160-c/Leica%25252050mm%252520Summilux.jpg","https://lh6.googleusercontent.com/-dwlPg83vzLg/URquylTVuFI/AAAAAAAAAbs/G6SyQ8b4YsI/s160-c/Leica%252520M8%252520%252528Front%252529.jpg","https://lh3.googleusercontent.com/-R3_EYAyJvfk/URquzQBv8eI/AAAAAAAAAbs/b9xhpUM3pEI/s160-c/Light%252520to%252520Sand.jpg","https://lh3.googleusercontent.com/-fHY5h67QPi0/URqu0Cp4J1I/AAAAAAAAAbs/0lG6m94Z6vM/s160-c/Little%252520Bit%252520of%252520Paradise.jpg","https://lh5.googleusercontent.com/-TzF_LwrCnRM/URqu0RddPOI/AAAAAAAAAbs/gaj2dLiuX0s/s160-c/Lone%252520Pine%252520Sunset.jpg","https://lh3.googleusercontent.com/-4HdpJ4_DXU4/URqu046dJ9I/AAAAAAAAAbs/eBOodtk2_uk/s160-c/Lonely%252520Rock.jpg","https://lh6.googleusercontent.com/-erbF--z-W4s/URqu1ajSLkI/AAAAAAAAAbs/xjDCDO1INzM/s160-c/Longue%252520Vue.jpg","https://lh6.googleusercontent.com/-0CXJRdJaqvc/URqu1opNZNI/AAAAAAAAAbs/PFB2oPUU7Lk/s160-c/Look%252520Me%252520in%252520the%252520Eye.jpg","https://lh6.googleusercontent.com/-6WS5DoCGuOA/URqu5qx1UgI/AAAAAAAAAbs/giMw2ixPvrY/s160-c/Open%252520Sky.jpg","https://lh6.googleusercontent.com/-u8EHKj8G8GQ/URqu55sM6yI/AAAAAAAAAbs/lIXX_GlTdmI/s160-c/Orange%252520Sunset.jpg","https://lh6.googleusercontent.com/-74Z5qj4bTDE/URqu6LSrJrI/AAAAAAAAAbs/XzmVkw90szQ/s160-c/Orchid.jpg","https://lh6.googleusercontent.com/-lEQE4h6TePE/URqu6t_lSkI/AAAAAAAAAbs/zvGYKOea_qY/s160-c/Over%252520there.jpg","https://lh5.googleusercontent.com/-cauH-53JH2M/URqu66v_USI/AAAAAAAAAbs/EucwwqclfKQ/s160-c/Plumes.jpg","https://lh3.googleusercontent.com/-eDLT2jHDoy4/URqu7axzkAI/AAAAAAAAAbs/iVZE-xJ7lZs/s160-c/Rainbokeh.jpg","https://lh5.googleusercontent.com/-j1NLqEFIyco/URqu8L1CGcI/AAAAAAAAAbs/aqZkgX66zlI/s160-c/Rainbow.jpg","https://lh5.googleusercontent.com/-DRnqmK0t4VU/URqu8XYN9yI/AAAAAAAAAbs/LgvF_592WLU/s160-c/Rice%252520Fields.jpg","https://lh3.googleusercontent.com/-hwh1v3EOGcQ/URqu8qOaKwI/AAAAAAAAAbs/IljRJRnbJGw/s160-c/Rockaway%252520Fire%252520Sky.jpg",};}
Main. xml is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ListView></RelativeLayout>
Listviewitem. xml is as follows:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/imageview" android:layout_width="70dip" android:layout_height="70dip" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="130dip" android:layout_marginTop="25dip" /> </LinearLayout>