How to scroll at the bottom of a ListView list programmatically?

After calling notifydatasetchanged(); I want to scroll the list at the bottom so that the user sees the last entry in Listview.

(I am writing a chat module, so for this purpose I need the last entry at the bottom of the list to be visible)

Can anyone advise me how to do this?

+41
android listview
Mar 29 '10 at 9:56
source share
3 answers

Try

 listView.post(new Runnable(){ public void run() { listView.setSelection(listView.getCount() - 1); }}); 

A β€œpost” seems to be required sometime in my experience, especially if you recently updated the list.

+70
Mar 29 '10 at 10:10
source share

I know that this was answered, and you answered, and that was more than a year ago. But the best way to do this is through transcription. For a demonstration, see Android API Demo under Views> Lists> Transcript.

In the list view in XML, you should set the following.

 android:stackFromBottom="true" android:transcriptMode="alwaysScroll" 

It will always work when you call notifyDataSetChanged() . You can set android:transcriptMode to normal instead if you want to get even better results for chat applications: it will scroll to the bottom only if the last item has already been in sight. Thus, your users can view the previous chat without interruption when other users communicate.

+81
Aug 10 2018-11-11T00:
source share

I know that it is very late to answer, but maybe this will help someone. Using android:transcriptMode="alwaysScroll" will cause the listview to scroll down (like we used android:stackFromBottom="true" ), even if you try to scroll to the top, which usually takes most of the time. Therefore, instead of android:transcriptMode="alwaysScroll you can use android:transcriptMode="normal , which will behave similarly to the requirement of the chat application and will not always force the scroll list if the user wants to see the contents at the top.

+1
Jan 24 '16 at 17:56
source share



All Articles