开发者

How to scroll to the bottom of ListView programmatically?

开发者 https://www.devze.com 2022-12-24 06:14 出处:网络
After calling notifydatasetchanged(); I want to scroll to the bottom of list so that user see the last record in the Listview.

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 module so for that purpose I need latest record at the bottom of list to be v开发者_C百科isible)

Can any one guide me how to achieve this?


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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消