Android Implementation ListView Asynchronous Loading method (improved version) _android

Source: Internet
Author: User

This example describes the Android implementation of ListView asynchronous loading. Share to everyone for your reference, specific as follows:

@Override public View getview (int position, View Convertview, viewgroup parent) {.....
         Viewholder VH = null;
      ............
      VH.mImageView.setTag (position); VH.mThumb.setImageDrawable (imageloader.loaddrawable (position, new Imagecallback () {public void Imageloa Ded (drawable imagedrawable, int position) {ImageView Imageviewbytag = (imageview) Mlist.findviewwithtag (posit
            ION);
              if (Imageviewbytag!= null) {imageviewbytag.setimagedrawable (imagedrawable);
  }
            }
         }));
  private static Lrucache<long, drawable> Mcache = new Lrucache<long, drawable> (100);
      public class Asyncimageloader {public drawable loaddrawable (final int position, final Imagecallback callback) {
      Drawable d = null;
      D = mcache.get (position); if (d = = null) {final Handler handler=new Handler () {@Override public void Handlemessage (Mess
 Age msg) {           Callback.imageloaded ((drawable) msg.obj, position);
        }
        }; Thread t = new Thread () {public void run () {drawable drawable = utils.getdrawable (Mcontext, Positio
            N, 1, Mmydefaulticon); drawable value = mcache.get (position);//cache may have been changed, so take a second if (value = = null) {Mcache.put P
            Osition, drawable);
            else {drawable = value;
          } handler.sendmessage (Handler.obtainmessage (0,drawable));
        };
        };
        T.setpriority (thread.min_priority);
        T.start ();
      return Mmydefaulticon;
      else {return D;
  }} public interface imagecallback{public void imageloaded (drawable imagedrawable, int position);

 }

This was modified on the basis of a previous article http://www.jb51.net/article/90408.htm. Mainly to join the LRUCache

It was later found that the method was not very efficient because it was constantly opening up new threads. Finally, a background thread is added, and the task is removed from the LIFO queue.

For more information on Android-related content readers can view the site: "Android View tips Summary", "Android Programming activity Operating Skills Summary", "Android operation SQLite Database Skills Summary", " Android operation JSON format data tips summary, "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", " Android Resource Operation tips Summary and the "Android Controls usage Summary"

I hope this article will help you with the Android program.

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.