Add new items to top of list view on Android?

后端 未结 7 1523
梦谈多话
梦谈多话 2020-11-30 03:19

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 tha

7条回答
  •  我在风中等你
    2020-11-30 03:41

    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);
        }
    }
    

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

提交回复
热议问题