I'm using an OnScrollListener to dynamically add items to a ListView when the user scrolls to the bottom. After I add the data to the adapter and call notifyDataSetChanged though, the ListView goes back up to the top. Ideally, I would like to retain the position in the ListView. Any thoughts on how I should go about doing this?
Could this be what you want?
// save index and top position int index = mList.getFirstVisiblePosition(); View v = mList.getChildAt(0); int top = (v == null) ? 0 : v.getTop(); // notify dataset changed or re-assign adapter here // restore the position of listview mList.setSelectionFromTop(index, top);
EDIT 28/09/2017:
The API has changed quite a bit since 2015. It is similar, but now it would be:
// save index and top position int index = mList.FirstVisiblePosition; //This changed View v = mList.getChildAt(0); int top = (v == null) ? 0 : v.Top; //this changed // notify dataset changed or re-assign adapter here // restore the position of listview mList.setSelectionFromTop(index, top);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With