1.
mListView.setOnScrollListener(new MyOnScrollListener());
2.
class MyOnScrollListener implements OnScrollListener {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
int firstVisiblePosition = mListView.getFirstVisiblePosition();
View firstView = mListView.getChildAt(0);
int firstViewTop = (firstView == null) ? 0 : firstView.getTop();
CacheUtils.putString(mActivity, "firstVisiblePosition_firstViewTop",
firstVisiblePosition + "," + firstViewTop);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
}
/** 恢复listView的滚动位置 */
String args = CacheUtils.getString(mActivity, "firstVisiblePosition_firstViewTop", ",");
if (!args.equals(",")) {
String[] split = args.split(",");
int arg0 = Integer.parseInt(split[0]);
int arg1 = Integer.parseInt(split[1]);
mListView.setSelectionFromTop(arg0, arg1);
}