Keep the current position of the listview.

Source: Internet
Author: User

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.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.