Android ListView add items to top without list view scroll [duplicate]
Asked Answered
W

3

23

I have a listView and I want to add new items to the top of the list view but I dont want list view to scroll its content. I want user to look at the same item as he was looking before new items were added.

This is how I add new items to ListView:

this.commentsListViewAdapter.addRangeToTop(comments);
this.commentsListViewAdapter.notifyDataSetChanged();

and this is addRangeToTop method:

public void addRangeToTop(ArrayList<Comment> comments)
{
    for (Comment comment : comments)
    {
        this.insert(comment, 0);        
    }
}

this is my listView:

<ListView
    android:id="@+id/CommentsListView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/AddCommentLayout" 
    android:stackFromBottom="true" >        
</ListView>

What I want to do is to load old comments when user scrolls to the top.

Thank you for your help.

Willena answered 24/3, 2013 at 10:41 Comment(0)
W
29

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);
Willena answered 24/3, 2013 at 11:26 Comment(0)
I
9

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

Ia answered 26/1, 2015 at 12:2 Comment(2)
this answer should be marked correct.Inviting
For recyclerview the answer is https://mcmap.net/q/391464/-is-there-a-recyclerview-equivalent-to-listview-39-s-transcriptmode-alwaysscrollMauretania
I
3

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

Improvement answered 24/3, 2013 at 10:51 Comment(1)
thank you, this works somehow, but not very well. It scrolls two items up. I think it is because List view does not scroll by items but by pixels, and setSelection scrolls exactly to the item.Willena

© 2022 - 2024 — McMap. All rights reserved.