Android Practical tips: Android implementation ListView loading pictures asynchronously

Source: Internet
Author: User

For the ListView asynchronous load Picture This problem, wheat college Android development teacher said a very practical method, wheat college Android development teacher says that anything is to get pictures through the network general Use this method is better, the user experience is good, the following said the implementation method, Paste the code of the Main method first:

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 the main method to achieve the asynchronous acquisition of pictures, SoftReference is a soft reference, is to better for the system to reclaim variables, duplicate URLs directly return the existing resources, implement the callback function, let the data succeed, update 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 a sub-element layout that assists in getting 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 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 implementation of the ListView adapter, there is a trick is Imageview.settag (IMAGEURL), Settag is to store data, so as to ensure that 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>



Android Practical tips: Android implementation ListView loading pictures asynchronously

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.