Android developers: Make the ListView slide smoothly

Source: Internet
Author: User

The key to smooth sliding the ListView is to keep the main thread of the application (UI thread) from the heavy processing. Make sure that you have any hard drive access, network access, or SQL access in a separate thread. In order to test the status of your application program, you can start Strictmode.

  Use background threads   ——————————————————————————————————————————————————————————————   Use a background thread ("worker thread") to remove from the main thread , so it can focus on drawing the UI. In many cases, use Asytask to provide an easy way to perform your work outside of the main thread. The Asynctask automatic queue saves all execute () requests and executes them consecutively. This behavior is global to a process and means you don't have to worry about creating your own thread pool.    in the example code below, Asynctask is used to download pictures in a background thread and then display them to the UI once it's done. When they are downloaded, it also shows a progress bar in the location of the picture.  [java]//Using a asynctask to load the slow images in a background thread   new Asynctask<viewholder, Void, bitmap> () {      private Viewholder v;       @Override   &nbs P   protected Bitmap doinbackground (Viewholder ... params) {          v = params[0]; &nbs p;         return mfakeimageloader.getimage ();             @Override       protected void OnPostExecute ( Bitmap result) {          Super.onpostexecute (result),       &NBSp   if (v.position = = position) {             //If this item hasn ' t been rec Ycled already, hide the              //progress and set and show the image &nbsp ;             v.progress.setvisibility (View.gone);               v.icon.setvisibility (view.visible);               V.ICON.SETIMAGEBITMAP (result);          }      }   }.execute (holder) <span style= " Font-family:arial,sans-serif; font-size:10pt; Color:windowtext "> </span>   starting with Android3.0 (API level11), there is an extra feature in Asynctask so you can enable it to run in multiprocessor cores. Instead of calling the Execute () method, you can call the Executeonexecutor () method and then, based on the number of cores available, multiple requests can run at the same time.    saving views in view holder   —————————————————————————————————————————————————————————————— when sliding the listview , your code should often call the Findviewbyid () method, which slows performance. Even if adapteR returns a loop used in the fill view, and you always need to look for this element and update them. A good way to use the Findviewbyid () method is to use the "View holder" design model. The   viewholder object holds the view components of the layout markup area, so you can access them immediately without having to look for them repeatedly. First, you need to create a class to hold the view group you need. Example:  [java] static class Viewholder {    TextView text;     TextView timestamp;     ImageView icon;     ProgressBar Progress;     int position;   }    then populate the Viewholder and save it in this layout.  [java]viewholder holder = new Viewholder ();   holder.icon = (ImageView) Convertview.findviewbyid (r.id.listitem_image);   holder.text = (TextView) Convertview.findviewbyid (R.id.listitem_text);   holder.timestamp = (TextView) Convertview.findviewbyid (R.id.listitem_timestamp);   holder.progress = (ProgressBar) Convertview.findviewbyid (R.id.progress_spinner);   convertview.settag (holder);    Now you can quickly access each view, no need to find, save valuable processor time.

Android developers: Make the ListView slide smoothly

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.