android ListView点击item返回后listview滚动位置

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);
上一篇:[硬件项目] 1、汽车倒车雷达设计——基于API8108A芯片简易智能语音模块的设计与实现


下一篇:基于UML的公开招聘教师管理系统建模的研究和设计