Ir al contenido

Más visitados

Visitados recientemente


Making ListView Scrolling Smooth

The key to a smoothly scrolling ListView is to keep the application’s main thread (the UI thread) free from heavy processing. Ensure you do any disk access, network access, or SQL access in a separate thread. To test the status of your app, you can enable StrictMode.

Use a Background Thread

Using a background thread ("worker thread") removes strain from the main thread so it can focus on drawing the UI. In many cases, using AsyncTask provides a simple way to perform your work outside the main thread. AsyncTask automatically queues up all the execute() requests and performs them serially. This behavior is global to a particular process and means you don’t need to worry about creating your own thread pool.

In the sample code below, an AsyncTask is used to load images in a background thread, then apply them to the UI once finished. It also shows a progress spinner in place of the images while they are loading.

// Using an AsyncTask to load the slow images in a background thread
new AsyncTask<ViewHolder, Void, Bitmap>() {
    private ViewHolder v;

    protected Bitmap doInBackground(ViewHolder... params) {
        v = params[0];
        return mFakeImageLoader.getImage();

    protected void onPostExecute(Bitmap result) {
        if (v.position == position) {
            // If this item hasn't been recycled already, hide the
            // progress and set and show the image

Beginning with Android 3.0 (API level 11), an extra feature is available in AsyncTask so you can enable it to run across multiple processor cores. Instead of calling execute() you can specify executeOnExecutor() and multiple requests can be executed at the same time depending on the number of cores available.

Hold View Objects in a View Holder

Your code might call findViewById() frequently during the scrolling of ListView, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. A way around repeated use of findViewById() is to use the "view holder" design pattern.

A ViewHolder object stores each of the component views inside the tag field of the Layout, so you can immediately access them without the need to look them up repeatedly. First, you need to create a class to hold your exact set of views. For example:

static class ViewHolder {
  TextView text;
  TextView timestamp;
  ImageView icon;
  ProgressBar progress;
  int position;

Then populate the ViewHolder and store it inside the layout.

ViewHolder holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(;
holder.text = (TextView) convertView.findViewById(;
holder.timestamp = (TextView) convertView.findViewById(;
holder.progress = (ProgressBar) convertView.findViewById(;

Now you can easily access each view without the need for the look-up, saving valuable processor cycles.

Este sitio usa cookies para almacenar tus preferencias específicas de idioma y visualización.

Obtén las noticias y sugerencias más recientes para programadores de Android, que te ayudarán a lograr el éxito en Google Play.

* Campos obligatorios

¡Muy bien!

Sigue a Google Developers en WeChat

¿Deseas explorar este sitio en ?

Solicitaste ver la página en , pero tu preferencia de idioma actual para este sitio es .

¿Quieres cambiar tu preferencia de idioma y explorar este sitio en ? Si deseas cambiarla en otro momento, usa el menú de idioma que se encuentra en la parte inferior de cada página.

Esta clase requiere un nivel de API igual o superior a

El documento está oculto porque el nivel de API que seleccionaste para la documentación es . Puedes cambiar el nivel de API de la documentación con el selector que está sobre el menú de navegación izquierdo.

Para obtener más información sobre cómo especificar el nivel de API que requiere tu app, consulta el artículo sobre Compatibilidad con diferentes versiones de la plataforma.

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)