增加新项目的在Android列表视图顶部?视图、新项目、列表、Android

2023-09-12 07:22:09 作者:喝了酱油耍酒疯

Android已经在transcript模式以允许自动滚动列表视图时新的数据被添加到转接器的底部。

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.

有没有人有一些例如code其中一个名单不断增加的东西,总是被插在上面?我是在正确的轨道在这里?

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