[Android Pro] accurately record and restore the ListView slide position

Source: Internet
Author: User

Reference to:http://blog.csdn.net/welovesunflower/article/details/7926512

Work encountered a need to refresh the ListView after an operation on the ListView, but if it is refreshed directly, the ListView is displayed from the first column and the user experience is not good, so the method of recovering the sliding position of the listview is searched on the internet. The solution is as follows:

1:

//set the ListView to a record locationmlistview.setselection (mposition); Mlistview.setonscrolllistener (NewOnscrolllistener () {//called when the scrolling state changes@Override Public voidOnscrollstatechanged (Abslistview view,intscrollstate) {          //Save current Scroll to position without scrolling        if(Scrollstate = =onscrolllistener.scroll_state_idle) {mposition=mlistview.getfirstvisibleposition (); }      }   //Called when scrolling@Override Public voidOnscroll (Abslistview view,intFirstvisibleitem,intVisibleItemCount,intTotalitemcount) {      }  });

2:

//setting up listeners for a ListViewBackgroundlist.setonscrolllistener (scrolllistener);PrivateOnscrolllistener Scrolllistener =NewOnscrolllistener () {@Override Public voidOnscroll (Abslistview arg0,intArg1,intArg2,intArg3) {} @Override Public voidOnscrollstatechanged (Abslistview view,intscrollstate) {                        if(Scrollstate = =onscrolllistener.scroll_state_idle) {                                //Scrollpos Record the position of a row at the top of the currently visible listScrollpos =mylist.getfirstvisibleposition (); }                        if(ArrayList! =NULL) {View v=mylist. Getchildat (0); ScrollTop= (v==NULL)?0: V.gettop (); }}}; restore location after calling Setadapter: MyList. Setselectionfromtop (Scrollpos, scrolltop);

[Android Pro] accurately record and restore the ListView slide position

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.