1

I got a ListView displaying everything that is in specific category and a service that is pulling data from a web server. I am presenting a lot of data and if I pull the data anew, feed it to the adapter and notify of the data change the whole listview gets refreshed. On one side there is no issue with that, but I wish the user to maintain the same position he has left on the screen.

Is it possible to keep the same offset or insert just 1 row in a table without moving the screen to the top of the list?

user3629316
  • 1,125
  • 2
  • 9
  • 8

1 Answers1

0

You need to maintain two variables one for current selected index and second for the top position of selected item from top of the list View.

int listSelectedIndex,top;

Use following method to save the current list position. You should call this method before notifying the data change.

private void saveListPosition() {

        listSelectedIndex = listView.getFirstVisiblePosition();
        View v = lstQuoteProducts.getChildAt(0);
        top = (v == null) ? 0 : v.getTop();
    }

After notify data change call following method to reset the saved position of list.

listView.setSelectionFromTop(listSelectedIndex, top);