how to maintain scroll position of listview when it updates how to maintain scroll position of listview when it updates json json

how to maintain scroll position of listview when it updates


It is easier to maintain scroll position by calling notifydatasetchanged() only. The problem there is that you are creating a new adapter every time the data gets updated... you should do something like this:

if(listView.getAdapter()==null)   listView.setAdapter(myAdapter);else{   myAdapter.updateData(myNewData);  //update adapter's data   myAdapter.notifyDataSetChanged(); //notifies any View reflecting data to refresh}

This way, your listview will mantain the scrolling position.

In case you want to scroll to a new position, use:

list.smoothScrollToPosition(int position);


In case for some reason you don't want to call notifyDataSetChanged(), the you can maintain the position by using setSelectionFromTop()

Before updating the adaptor:

lastViewedPosition = listView.getFirstVisiblePosition();//get offset of first visible viewView v = listView.getChildAt(0);topOffset = (v == null) ? 0 : v.getTop();

After updating the adaptor:

listView.setSelectionFromTop(lastViewedPosition, topOffset);


list.smoothScrollToPosition(int position);     //my favorite :)

It may also help you to scroll nice'n'smooth to a particular item