本文介绍了在 Android 上将新项目添加到列表视图的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 具有 转录模式允许在将新数据添加到适配器时自动将列表视图滚动到底部.

Android has the transcript mode to allow to automatically scroll a list view to the bottom when new data is added to the adapter.

这是否可以以某种方式颠倒,以便新项目自动添加到列表顶部(逆转录模式")

Can this be somehow reversed so that new items are automatically added at the top of the list ("inverse transcript mode")

方法 stackFromBottom 似乎是正确的,但不会在输入更改时自动滚动.

Method stackFromBottom seems about right, but does not do the auto-scrolling on input change.

有没有人有一些示例代码,其中列表不断添加总是插入顶部的内容?我在正确的轨道上吗?

Does anyone have some example code where a list is constantly adding stuff that gets always inserted at the top? Am I on the right track here?

更新

感谢您的回答,这让我思考了更多.实际上.我想让新条目出现在顶部,但屏幕仍然显示用户正在查看的项目.用户应该主动滚动到顶部以查看新项目.所以我想转录模式不是我想要的.

Thanks for the answers, that made me think more. Actually. I want to have new entries to appear at the top, but the screen still show the item the user is looking at. The user should actively scroll to the top to view the new items. So I guess that transcript mode is not what I want.

推荐答案

嗯,好吧,如果我要尝试这个,我会做如下的事情:

Hmm, well, if I was going to try this, I'd do something like the following:

List items = new ArrayList();

//some fictitious objectList where we're populating data
for(Object obj : objectList) {
    items.add(0, obj);
    listAdapter.notifyDataSetChanged();
}

listView.post(new Runnable() {
    @Override
    public void run() {
        listView.smoothScrollToPosition(0);
    }
}

我不确定这是否可行,但似乎合乎逻辑.基本上,只需确保将项目添加到列表的开头(位置 0),刷新列表适配器,然后滚动到位置 (0, 0).

I don't know for certain that this will work, but it seems logical. Basically, just make sure to add the item at the beginning of the list (position 0), refresh the list adapter, and scroll to position (0, 0).

这篇关于在 Android 上将新项目添加到列表视图的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:31