I have a list view in an Android (JB) app where clicking on a row replaces the list view with another view.
Pressing separate button, adds the list view back to the activity (the idea is that the parent container toggles between the list view and the contents of a single row).
The problem happens when I scroll to a row near the bottom and press it (to replace the list view). After hitting the other button to show the list view again, everything looks fine (including the row states and the scroll position). But as soon as I try to scroll again, it resets the list view to the top. Scrolling after that works normally.
The data source for the list view doesn't change at all. Weirdly, if I change the orientation of the tablet, then scrolling for the first time doesn't reset the list view to the top.
I'm guessing it has something to do with removing the view and then adding back that same view (which is done by calling removeAllViews and addView on the parent).
I'm not sure but I think when you call removeAllViews for some reason it can't save the previous state. I'm not sure again if it is the best way to solve the problem but what you can do is to save your scroll position before remove it, and after add your views again you can set it.
Something like this:
int posX = listView.getScrollX();
//remove your view
and after:
//add your views
listView.setScrollX(posX);