且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Android 上将新项目添加到列表视图的顶部?

更新时间:2023-11-28 18:12:16

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

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