Listview Desplácese hasta el final de la lista después de actualizar la lista


Me gustaría asegurarme de que la lista se desplace hasta el final, después de haber actualizado la listview usando ListAdapter, para que muestre el último elemento introducido en la lista. ¿Cómo puedo hacer esto ?

Lo intenté pero no tuve suerte:

lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);

Gracias

Author: jramirez, 2010-08-31

8 answers

Supongamos que sabe cuándo han cambiado los datos de la lista, puede decirle manualmente a la lista que se desplace hasta la parte inferior configurando la selección de la lista en la última fila. Algo como:

private void scrollMyListViewToBottom() {
    myListView.post(new Runnable() {
        @Override
        public void run() {
            // Select the last row so it will scroll into view...
            myListView.setSelection(myListAdapter.getCount() - 1);
        }
    });
}
 396
Author: Mason Lee,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-24 16:37:57

Necesita usar estos parámetros en su vista de lista:

  • Desplazamiento lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);

  • Establecer la cabeza de la lista a la parte inferior lv.setStackFromBottom(true);

También puede establecer estos parámetros en XML, por ejemplo. así:

<ListView
   ...
   android:transcriptMode="alwaysScroll"
   android:stackFromBottom="true" />
 229
Author: dev.serghini,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-13 11:36:41

Una combinación de TRANSCRIPT_MODE_ALWAYS_SCROLL y setSelection hizo que funcionara para mí

ChatAdapter adapter = new ChatAdapter(this);

ListView lv = (ListView) findViewById(R.id.chatList);
lv.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
lv.setAdapter(adapter);

adapter.registerDataSetObserver(new DataSetObserver() {
    @Override
    public void onChanged() {
        super.onChanged();
        lv.setSelection(adapter.getCount() - 1);    
    }
});
 36
Author: Wärting,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-27 09:43:40

He tenido éxito usando esto en respuesta a un clic de botón, así que supongo que también puedes usarlo después de actualizar tu contenido:

myListView.smoothScrollToPosition(theListAdapter.getCount() -1);
 24
Author: Álex,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-08 20:12:44

Usando : Pon la cabecera de la lista en la parte inferior lv.setStackFromBottom(true);

Funcionó para mí y la lista se desplaza hacia abajo automáticamente cuando se pone en visibilidad por primera vez. La lista entonces se desplaza como debería con TRANSCRIPT_MODE_ALWAYS_SCROLL.

 10
Author: Kevin Parker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-10 07:11:15

Para obtener esto en un ListFragment:

getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL); 
getListView().setStackFromBottom(true);`

Agregó esta respuesta porque si alguien hace una búsqueda en Google por el mismo problema con ListFragment, simplemente encuentra esto..

Saludos

 9
Author: Klatschen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-07 07:26:35

Utilizo

setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);

Para agregar entradas en la parte inferior, y las entradas anteriores se desplazan por la parte superior, como una transcripción de chat

 7
Author: Plugie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-16 14:13:56

El modo de transcripción es lo que desea y es utilizado por Google Talk y la aplicación SMS/MMS. ¿Está llamando correctamente a notifyDataSetChanged () en su adaptador cuando agrega elementos?

 4
Author: Romain Guy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-08-31 06:42:35