整体思路:滑动lictview时,记录listview的位置,定位时定位到该位置。
1、添加全局变量
private int scrolledX = 0;
private int scrolledY = 0;
2、Oncreate()添加滚动事件监听
ClassesLV.setOnScrollListener(new OnScrollListener() {
/**
* 滚动状态改变时调用
*/
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
try {
scrolledX = view.getFirstVisiblePosition();
Log.i("scroll X", String.valueOf(scrolledX));
scrolledY = view.getChildAt(0).getTop();
Log.i("scroll Y", String.valueOf(scrolledY));
} catch (Exception e) {
}
}
}
//
});
3、activity 重新启动时,恢复原先的定位。
public void onBack() {
ClassesLV.setSelectionFromTop(scrolledX, scrolledY);
}
protected void onResume() {
// ClassesLV.scrollTo(scrolledX, scrolledY);
onBack();
// initDataFromNet();
isClick = 0;
super.onResume();
}