/** * Refreshes the content of the parent list view while maintaining the current vertical position * in the list view. */ private void refreshListView(ViewGroup parent, Group updatedGroup) { // Get position of first and last visible list items AbsListView parentListView = (AbsListView) parent.findViewById(android.R.id.list); int firstVisible = parentListView.getFirstVisiblePosition(); int lastVisible = parentListView.getLastVisiblePosition(); // Populate list view mItems.clear(); List<Group> groups = Group.getAllSortedByDisplayOrder(); for (Group group : groups) { mItems.add(group.name); } notifyDataSetChanged(); // Handle case where item is at visible top and moved up. if (firstVisible >= updatedGroup.getDisplayOrder()) { parentListView.setSelection(updatedGroup.getDisplayOrder() - 1); // Handle case where item is at visible bottom and moved down. } else if (lastVisible < updatedGroup.getDisplayOrder()) { parentListView.setSelection(firstVisible + 1); // Handle move cases in the middle of viewable listview. } else { parentListView.setSelection(firstVisible); } }