Android Listview 更新列表后滚动到列表末尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3606530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:56:37  来源:igfitidea点击:

Listview Scroll to the end of the list after updating the list

androidlistviewscroll

提问by jramirez

I would like to make sure that the list is scrolled all the way to the bottom, after I have updated the listview by using listAdapter, so that it displays the last element entered in the list. How can I do this ?

在使用 listAdapter 更新列表视图后,我想确保列表一直滚动到底部,以便它显示在列表中输入的最后一个元素。我怎样才能做到这一点 ?

I tried this but no luck:

我试过这个,但没有运气:

lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);

thank you

谢谢你

回答by Mason Lee

Supposing you know when the list data has changed, you can manually tell the list to scroll to the bottom by setting the list selection to the last row. Something like:

假设您知道列表数据何时发生更改,您可以通过将列表选择设置为最后一行来手动告诉列表滚动到底部。就像是:

private void scrollMyListViewToBottom() {
    myListView.post(new Runnable() {
        @Override
        public void run() {
            // Select the last row so it will scroll into view...
            myListView.setSelection(myListAdapter.getCount() - 1);
        }
    });
}

回答by dev.serghini

You need to use these parameters in your list view:

您需要在列表视图中使用这些参数:

  • Scroll lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);

  • Set the head of the list to it bottom lv.setStackFromBottom(true);

  • 滚动 lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);

  • 将列表的头部设置为底部 lv.setStackFromBottom(true);

You can also set these parameters in XML, eg. like this:

您还可以在 XML 中设置这些参数,例如。像这样:

<ListView
   ...
   android:transcriptMode="alwaysScroll"
   android:stackFromBottom="true" />

回答by W?rting

A combination of TRANSCRIPT_MODE_ALWAYS_SCROLL and setSelection made it work for me

TRANSCRIPT_MODE_ALWAYS_SCROLL 和 setSelection 的组合使它对我有用

ChatAdapter adapter = new ChatAdapter(this);

ListView lv = (ListView) findViewById(R.id.chatList);
lv.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
lv.setAdapter(adapter);

adapter.registerDataSetObserver(new DataSetObserver() {
    @Override
    public void onChanged() {
        super.onChanged();
        lv.setSelection(adapter.getCount() - 1);    
    }
});

回答by álex

I've had success using this in response to a button click, so I guess that you can use it too after updating your contents:

我已经成功地使用它来响应按钮点击,所以我想你也可以在更新你的内容后使用它:

myListView.smoothScrollToPosition(theListAdapter.getCount() -1);

回答by Klatschen

To get this in a ListFragment:

要在 ListFragment 中获取它:

getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL); 
getListView().setStackFromBottom(true);`

Added this answer because if someone do a google search for same problem with ListFragment he just finds this..

添加了这个答案,因为如果有人用 google 搜索 ListFragment 的相同问题,他就会发现这个..

Regards

问候

回答by Kevin Parker

Using : Set the head of the list to it bottom lv.setStackFromBottom(true);

使用:将列表的头部设置为底部 lv.setStackFromBottom(true);

Worked for me and the list is scrolled to the bottom automatically when it is first brought into visibility. The list then scrolls as it should with TRANSCRIPT_MODE_ALWAYS_SCROLL.

为我工作,列表在首次显示时会自动滚动到底部。然后列表会随 一起滚动TRANSCRIPT_MODE_ALWAYS_SCROLL

回答by Plugie

I use

我用

setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);

to add entries at the bottom, and older entries scroll off the top, like a chat transcript

在底部添加条目,旧条目从顶部滚动,如聊天记录

回答by ucMedia

The simplest solution is :

最简单的解决方案是:

    listView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    listView.setStackFromBottom(true);

回答by Romain Guy

The transcript mode is what you want and is used by Google Talk and the SMS/MMS application. Are you correctly calling notifyDatasetChanged() on your adapter when you add items?

Google Talk 和 SMS/MMS 应用程序使用转录模式。添加项目时,您是否正确调用了适配器上的 notifyDatasetChanged()?