Add new items to top of list view on Android?

后端 未结 7 1487
梦谈多话
梦谈多话 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).

    0 讨论(0)
  • 2020-11-30 03:41

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

    0 讨论(0)
  • 2020-11-30 03:44

    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 讨论(0)
  • 2020-11-30 03:52

    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);
                }
                });
    
    0 讨论(0)
  • 2020-11-30 03:53

    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();
    
    0 讨论(0)
  • 2020-11-30 03:53

    you should try that (list position and refresh adapter)

    list.add(0,editTextSName.getText().toString());
    adapter.notifyDataSetChanged();
    
    0 讨论(0)
提交回复
热议问题