Android ListView add items to top without list view scroll

This question already has an answer here:

  • Retaining position in ListView after calling notifyDataSetChanged

    6 answers

  • android.util.AndroidRuntimeException: You cannot combine swipe dismissal and the action bar
  • How to handle multiple fragment interaction listeners in one Activity properly?
  • Error in launching AVD with AMD processor
  • How to hide scrollbars in Android WebView through React Native
  • Multi-level ExpandableListView in Android
  • Android : What should I do instead of using a deprecated function(getwidth() )?
  • Related posts:

    Best way to work with dates in Android SQLite
    Byte-size enum in Java
    Android SQLite Database Unit Testing
    How to include and use ZXing Library in Android with eclipse?
    Reading a textfile using InputStream
    Offline Android Dev Guide
  • how to Manage Free version?
  • How to prepare KML file for Android Emulator Control?
  • How can I force memory pressure for Android debugging?
  • Custom view constructor does not get called on android 5.0.2
  • onCreate() and onCreateView() invokes a lot more than required (Fragments)
  • getActionBar() returns null
  • 3 Solutions collect form web for “Android ListView add items to top without list view scroll”

    I have found solution here Retaining position in ListView after calling notifyDataSetChanged

    Sorry for duplicate question.
    The final code is this:

        int index = this.commentsListView.getFirstVisiblePosition() + comments.size();
        View v = this.commentsListView.getChildAt(commentsListView.getHeaderViewsCount());
        int top = (v == null) ? 0 : v.getTop();         
    
        this.commentsListViewAdapter.AddRangeToTop(comments);
        this.commentsListViewAdapter.notifyDataSetChanged();    
    
        this.commentsListView.setSelectionFromTop(index, top);
    

    May be this is what you are looking for:

    android:transcriptMode="normal"
    

    “This makes list automatically scroll to the bottom when a data set change notification is received and only if the last item is already visible on screen.” – as quoted here

    Also have a look at ListView’s method public void setSelection (int position). After you added new comments, and notified your adapter, you can use it to keep the current item selected.

    // Get the current selected index
    int previousSelectedIndex = yourListView.getSelectedItemPosition();
    
    // Change your adapter
    this.commentsListViewAdapter.AddRangeToTop(comments);
    this.commentsListViewAdapter.notifyDataSetChanged();
    
    
    // Determine how many elements you just inserted
    int numberOfInsertedItems = comments.size();
    
    // Update the selected position
    yourListView.setSelection(previousSelectedIndex + numberOfInsertedItems);
    

    NOTE: Code is untested. Good luck

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.