Keep the current position of the listview.
Keep the position of the listview slide. It is generally used to add comments in the listview sub-item layout or exit the current activity.
Use ListView. getFirstVisiblePosition () to obtain the position of the first Item currently visible, and set it in the rolling listener.
@ Override public void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {this. firstVisibleItem = firstVisibleItem; this. lastVisibleItem = firstVisibleItem + visibleItemCount; this. totalItemCount = totalItemCount;} @ Override public void onScrollStateChanged (AbsListView view, int scrollState) {this. scrollState = scrollState; if (scrollState = AbsListView. onScrollListener. SCROLL_STATE_IDLE) {position = this. getFirstVisiblePosition (); // obtain the index number in the total number of entries in the list. View firstVisibleItem = this. getChildAt (0); // obtain the first item in the visible list scrolledY = firstVisibleItem. getTop (); // obtain the position of the first list item relative to the top of the screen }}
Position and scrolledY are stored and called at key locations.
listView.setSelectionFromTop(position, scrolledY);(api 21)
Call the previous method to restore the current position of the listview.