ListView is a view that can display a series of items and can be scrolled, each row of item may contain a complex structure, may get some icon information from the network icons, etc., now the network speed to keep the ListView running well scrolling smooth is not done
So this information needs to be loaded asynchronously using multithreading
Classes that implement such a function
[Java]View Plaincopy
- 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 () {
- @Override
- 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) {
- // ...
- }
- Public interface Imagecallback {
- public void imageloaded (drawable imagedrawable, String imageUrl);
- }
- }
Note that SoftReference is used to cache images, allowing the GC to clean up images in the cache when needed. It works like this:
· Call Loaddrawable (IMAGEURL, Imagecallback), passing in an anonymous implementation of the Imagecallback interface
· If the picture does not exist in the cache, the picture is downloaded from a single thread and passed the Imagecallback callback at the end of the download
· If the picture does exist in the cache, it will return immediately and will not callback Imagecallback
We can then continue to optimize adapter using Viewholder to reduce some of the more time-consuming operations, such as inflate XML and Findviewbyid (), based on the way the 09google i/0 developer Conference mentions
[Java]View Plaincopy
- 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 ();
- }
- @Override
- 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);
- }
- }
- });
- Imageview.setimagedrawable (Cachedimage);
- //Set The text on the TextView
- TextView TextView = Viewcache.gettextview ();
- Textview.settext (Imageandtext.gettext ());
- return rowview;
- }
- }
Here we do not load after iamge directly set to the corresponding ImageView, but through tag lookup, here we re-use the view here is a ListView reference to find the implementation of the visible callback through tag
[C-sharp]View Plaincopy
- ImageView Imageviewbytag = (ImageView) listview.findviewwithtag (IMAGEURL);
- if (imageviewbytag! = null) {
- Imageviewbytag.setimagedrawable (imagedrawable);
- }
This reduces the use of Findviewbyid through Viewcatch.
[C-sharp]View Plaincopy
- 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 titleview;
- }
- Public ImageView Getimageview () {
- if (ImageView = = null) {
- ImageView = (ImageView) Baseview.findviewbyid (r.id.image);
- }
- return imageView;
- }
- }
Summary : Here is the main three-point optimization
- Bunch on a single line load picture
- the View in the cache line
Transferred from: http://blog.csdn.net/wanglong0537/article/details/6334005