1.Don't work when dynamically loading content

Parcelable state;

@Override
public void onPause() {
// Save ListView state @ onPause
Log.d(TAG, "saving listview state @ onPause");
state = listView.onSaveInstanceState();
super.onPause();
}
... @Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Set new items
listView.setAdapter(adapter);
...
// Restore previous state (including selected item index and scroll position)
if(state != null) {
Log.d(TAG, "trying to restore listview state..");
listView.onRestoreInstanceState(state);
}
}

2.在listview点击事件中保存位置信息

static int index ,top;
listview.setOnItemClickListener(new OnItemClickListener() { @SuppressLint("ResourceAsColor")
@Override
public void onItemClick(final AdapterView<?> parent, View view, final int position, long id) { index = listview.getFirstVisiblePosition();
View v = listview.getChildAt(0);
top = (v == null) ? 0 : (v.getTop() - listview.getPaddingTop()); }
});
listview.setAdapter(adapter);
// restore index and position
listview.setSelectionFromTop(index, top);
05-08 15:45