Android implements ListView asynchronous loading of pictures _android

Source: Internet
Author: User
Tags gettext

This article describes the Android implementation ListView method of asynchronously loading pictures. Share to everyone for your reference. Specifically as follows:

ListView asynchronous loading of pictures is a very practical way, usually to get pictures through the network generally use this method is better, the user experience is good, do not let the user wait, the following said implementation method, first affixed to the Main method code:

Package cn.wangmeng.test;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.lang.ref.SoftReference;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import Java.util.HashMap;
Import android.graphics.drawable.Drawable;
Import Android.os.Handler;
Import Android.os.Message;
 public class Asyncimageloader {private hashmap<string, softreference<drawable>> Imagecache;
  Public Asyncimageloader () {Imagecache = new hashmap<string, softreference<drawable>> (); Public drawable loaddrawable (final String imageUrl, final imagecallback imagecallback) {if (Imagecache.containskey) (i
   Mageurl)) {softreference<drawable> softreference = Imagecache.get (IMAGEURL);
   drawable drawable = Softreference.get ();
   if (drawable!= null) {return drawable; } final Handler Handler = new Handler () {public void Handlemessage (Message message) {Imagecallback.imageload
   Ed ((drawable) message.obj, IMAGEURL);
  }
  }; New Thread ({@Override public void run () {drawable drawable = Loadimagefromurl (IMAGEURL);
   Imagecache.put (IMAGEURL, New softreference<drawable> (drawable));
   Message message = Handler.obtainmessage (0, drawable);
   Handler.sendmessage (message);
  }}.start ();
  return null;
  public static drawable loadimagefromurl (String url) {URL m;
  InputStream i = null;
  try {m = new URL (URL);
  i = (InputStream) m.getcontent ();
  catch (Malformedurlexception E1) {e1.printstacktrace ();
  catch (IOException e) {e.printstacktrace ();
  } drawable D = Drawable.createfromstream (i, "src");
 return D;
  Public interface Imagecallback {public void imageloaded (drawable imagedrawable, String imageUrl);

 }
}

The above code is to achieve the main method of obtaining pictures asynchronously, SoftReference is a soft reference, is to better for the system to reclaim variables, duplicate URLs directly return the existing resources, implement callback functions, so that the data successfully updated to the UI thread.

Several auxiliary class files:

Package cn.wangmeng.test;
public class ImageAndText {
 private String imageUrl;
 private String text;
 Public ImageAndText (String imageUrl, string text) {
  this.imageurl = IMAGEURL;
  This.text = text;
 Public String Getimageurl () {return
  imageUrl;
 }
 Public String GetText () {return
  text;
 }
}

Package cn.wangmeng.test;
Import Android.view.View;
Import Android.widget.ImageView;
Import Android.widget.TextView;
public class Viewcache {
 private View baseview;
 Private TextView TextView;
 Private ImageView ImageView;
 Public Viewcache (View baseview) {
  this.baseview = Baseview;
 }
 Public TextView Gettextview () {
  if (TextView = null) {
  TextView = (TextView) Baseview.findviewbyid (R.id.text) ;
  }
  return textView;
 }
 Public ImageView Getimageview () {
  if (ImageView = null) {
  ImageView = (ImageView) Baseview.findviewbyid ( r.id.image);
  }
  Return ImageView
 }
}

Viewcache is the child element layout of the secondary fetch adapter:

Package cn.wangmeng.test;
Import java.util.List;
Import Cn.wangmeng.test.AsyncImageLoader.ImageCallback;
Import android.app.Activity;
Import android.graphics.drawable.Drawable;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.ArrayAdapter;
Import Android.widget.ImageView;
Import Android.widget.ListView;
Import Android.widget.TextView;
 public class Imageandtextlistadapter extends arrayadapter<imageandtext> {private ListView ListView;
 Private Asyncimageloader Asyncimageloader; Public Imageandtextlistadapter (activity activity, list<imageandtext> imageandtexts, ListView ListView) {super (
  Activity, 0, imageandtexts);
  This.listview = ListView;
 Asyncimageloader = new Asyncimageloader ();
  Public View GetView (int position, View Convertview, ViewGroup parent) {Activity activity = (activity) getcontext ();
  Inflate the views from XML View Rowview = Convertview;
  Viewcache Viewcache; if (Rowview= = null) {Layoutinflater inflater = Activity.getlayoutinflater ();
  Rowview = inflater.inflate (R.layout.image_and_text_row, NULL);
  Viewcache = new Viewcache (Rowview);
  Rowview.settag (Viewcache);
  else {Viewcache = (Viewcache) rowview.gettag ();
  } imageandtext ImageAndText = GetItem (position);
  Load the image and set it on the ImageView String imageUrl = Imageandtext.getimageurl ();
  ImageView ImageView = Viewcache.getimageview ();
  Imageview.settag (IMAGEURL); drawable cachedimage = asyncimageloader.loaddrawable (ImageUrl, New Imagecallback () {public void imageloaded (drawable im
   Agedrawable, String imageUrl) {ImageView Imageviewbytag = (imageview) listview.findviewwithtag (IMAGEURL);
   if (Imageviewbytag!= null) {imageviewbytag.setimagedrawable (imagedrawable);
  }
  }
  });
  if (cachedimage = = null) {Imageview.setimageresource (r.drawable.default_image);
  }else{imageview.setimagedrawable (cachedimage); }//Set the text on the TextView TEXTVIew TextView = Viewcache.gettextview ();
  Textview.settext (Imageandtext.gettext ());
 return rowview;

 }
}

Imageandtextlistadapter is the adapter that implements ListView, and there is a trick in Imageview.settag (IMAGEURL), which is to store the data, to ensure that when the callback function ListView to update their corresponding item, we read carefully to know.

Finally post the layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:orientation=" horizontal "
  android:layout_width=" fill_parent "
  android:layout_" height= "Wrap_content" >
 <imageview android:id= "@+id/image" android:layout_width= "Wrap_content"
   android:layout_height= "wrap_content"
   />
 <textview android:id= "@+id/text"
   android:layout _width= "Wrap_content"
   android:layout_height= "wrap_content"/>
</LinearLayout>

The screenshot of the running effect is as follows:

I hope this article will help you with the C # program.

Related Article

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.