Android Development ListView Asynchronous loading of pictures

Source: Internet
Author: User
Tags gettext

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:

The code is as follows Copy 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 (IMAGEURL)) {
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.imageloaded ((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:

The code is as follows Copy Code
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 a child element layout that assists in obtaining adapter

The code is as follows Copy Code
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 imagedrawable, 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:

  code is as follows copy code
<?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>
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.