Heiko Rupp Heiko Rupp - 1 year ago 57
Android Question

Add new items to top of list view on 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")

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.

Answer Source

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

listView.post(new Runnable() {
    public void run() {

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download