如何以编程方式滚动到ListView的底部?

时间:2022-08-24 23:26:15

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

在调用notifydatasetchanged()之后;我想滚动到列表的底部,以便用户查看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?

任何人都可以指导我如何实现这一目标吗?

3 个解决方案

#1


72  

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.

根据我的经验,似乎需要“帖子”,特别是如果您最近更新了列表。

#2


82  

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.

我知道这已经得到了解答,你接受了答案,这是一年多以前的事了。但更好的方法是成绩单模式。有关演示,请参阅视图>列表>转录下的Android API演示。

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

您可以在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.

无论何时调用notifyDataSetChanged(),它都将始终有效。如果你想为聊天应用程序提供更好的结果,你可以将android:transcriptMode设置为normal:只有当最后一项已经在视图中时,它才会滚动到底部。这样,当其他用户聊天时,您的用户可以不间断地查看以前的聊天。

#3


1  

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.

我知道它的回答很晚,但可能会对某人有所帮助。使用android:transcriptMode =“alwaysScroll”将强制列表视图滚动到底部(因为这里我们使用了android:stackFromBottom =“true”),即使你将尝试滚动顶部,这通常需要大部分时间。因此,而不是android:transcriptMode =“alwaysScroll你可以使用android:transcriptMode =”normal,它的行为类似于聊天应用程序的要求,并且如果用户想要在顶部看到内容,则不会强制滚动列表。

#1


72  

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.

根据我的经验,似乎需要“帖子”,特别是如果您最近更新了列表。

#2


82  

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.

我知道这已经得到了解答,你接受了答案,这是一年多以前的事了。但更好的方法是成绩单模式。有关演示,请参阅视图>列表>转录下的Android API演示。

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

您可以在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.

无论何时调用notifyDataSetChanged(),它都将始终有效。如果你想为聊天应用程序提供更好的结果,你可以将android:transcriptMode设置为normal:只有当最后一项已经在视图中时,它才会滚动到底部。这样,当其他用户聊天时,您的用户可以不间断地查看以前的聊天。

#3


1  

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.

我知道它的回答很晚,但可能会对某人有所帮助。使用android:transcriptMode =“alwaysScroll”将强制列表视图滚动到底部(因为这里我们使用了android:stackFromBottom =“true”),即使你将尝试滚动顶部,这通常需要大部分时间。因此,而不是android:transcriptMode =“alwaysScroll你可以使用android:transcriptMode =”normal,它的行为类似于聊天应用程序的要求,并且如果用户想要在顶部看到内容,则不会强制滚动列表。