How to scroll to the bottom of ListView programmatically?

前端 未结 3 1390
借酒劲吻你
借酒劲吻你 2020-12-04 16:40

After calling notifydatasetchanged(); I want to scroll to the bottom of list so that user see the last record in the Listview.

(I am writing Chat modul

相关标签:
3条回答
  • 2020-12-04 17:05

    I know its very late to answer but may be it will help someone. Using android:transcriptMode="alwaysScroll" will force the listview to scroll to bottom (as here we have used android:stackFromBottom="true") even if you'll try to scroll top which usually required most of the time. So instead of android:transcriptMode="alwaysScroll you can use android:transcriptMode="normal which will behave similar to the requirement of chat app and will not force the list of scroll always if the user want to see the content at the top.

    0 讨论(0)
  • 2020-12-04 17:12

    Try

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

    The 'post' seems to be required sometime in my experience, particularly if you have very recently updated the list.

    0 讨论(0)
  • 2020-12-04 17:24

    I know that this has been answered and you took the answer and it was more than a year ago. But a better way to do this is Transcript Mode. For a demo, see the Android API Demo under Views > Lists > Transcript.

    You would set the following on your list view in the XML.

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

    It will always work whenever you call notifyDataSetChanged(). You can set android:transcriptMode to normal instead if you want an even better result for chat applications: it will scroll to the bottom only if the last item was already in view. That way your users can view the previous chat without interruption when other users chat.

    • transcript mode
    • stack from bottom
    0 讨论(0)
提交回复
热议问题