Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

android - How to scroll to the bottom of ListView programmatically?

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 visible)

Can any one guide me how to achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...