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