33

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?

Update

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.

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
  • 1
    Do you want it to 'scroll' (smooth or otherwise) or do you simply want it to jump/stay with the most recently added item as the first (top) item in the list. If it's the second behaviour you want, just call the ListView 'setSelection(0)' method after you add a new item at index 0 in your adapter. – Squonk May 05 '11 at 22:20
  • to do the smooth scroll wait 100 millis then do your scroll , here is code example https://stackoverflow.com/a/37491888/6039240 – Amr Nov 05 '21 at 09:47

7 Answers7

48

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

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
9

instead of this:

items.add(edittext.getText().toString());
adapter.notifyDataSetChanged();

you should try that (works for me):

listview.post(new Runnable() {
            @Override
            public void run() {
                items.add(0, edittext.getText().toString());
                adapter.notifyDataSetChanged();
                listview.smoothScrollToPosition(0);
            }
            });
Matcoil
  • 890
  • 11
  • 23
elementstyle
  • 1,021
  • 1
  • 10
  • 17
  • 1
    Pardon me for asking a naive question, but how this solution is better than the accepted answer? – Darpan Aug 20 '15 at 08:55
7

Shouldn't it be enough to just add a smoothScrollToPosition(0) whenever stuff gets added to the ListView? Don't think there's an automatic scroll option.

Femi
  • 64,273
  • 8
  • 118
  • 148
7

I spent several hours attempting to accomplish the same thing. Essentially, this acts like a chat app where the user scrolls up to view older messages at the top of the list.

The problem is that, you want to dynamically add another 50 or 100 records to the top but the scrolling should be continuous from where the prepended items were added.

The moment you do a notifyDataSetChanged, the ListView will automatically position itself at the first item in your data set and NOT at the position that preceded the position where the new items got inserted.

This makes it look like your list just jumped 50 or 100 records. I believe TranscriptMode set to normal is not the solution. The listview needs to function as a normal listview and you need to programmatically scroll to the bottom of the list to simulate the TranscriptMode as it functions under "normal".

Bitmap
  • 12,402
  • 16
  • 64
  • 91
Johann
  • 27,536
  • 39
  • 165
  • 279
2

Try to use

LinkedList items = new LinkedList<Object>();

//some fictitious objectList where we're populating data
for(Object obj : objectList) {
    items.addFirst(obj);
}
listAdapter.notifyDataSetChanged();
sonida
  • 4,411
  • 1
  • 38
  • 40
  • Don't use LinkedList for reading data, because the LinkedList is slower than ArrayList for reading data, but LinkedList faster for adding data in list. – Sinan Dizdarević Nov 02 '14 at 06:30
2

This resolves the problem:

...
ListView lv;
ArrayAdapter<String> adapter;
ArrayList<String> aList;
...
        lv = (ListView) findViewById(R.id.mylist);

        aList = new ArrayList<String>();
        adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, aList);
        lv.setAdapter(aAdapter);
        ...
        adapter.insert ("Some Text 1", 0);
        adapter.insert ("Some Text 2", 0);
        adapter.insert ("Some Text 3", 0);
        ...
0

you should try that (list position and refresh adapter)

list.add(0,editTextSName.getText().toString());
adapter.notifyDataSetChanged();
Alexan
  • 8,165
  • 14
  • 74
  • 101