返回到ListView时维护/保存/还原滚动位置我有很长的时间ListView在返回到前一个屏幕之前,用户可以滚动。当用户打开以下内容时ListView同样,我希望将列表滚动到与以前相同的位置。对于如何实现这一点,有什么想法吗?
3 回答
慕慕森
TA贡献1856条经验 获得超17个赞
// save index and top positionint index = mList.getFirstVisiblePosition();View v = mList.getChildAt(0);int top = (v == null) ? 0 : (v.getTop() - mList.getPaddingTop());// ...// restore index and positionmList.setSelectionFromTop(index, top);
ListView.getFirstVisiblePosition()
ListView.getChildAt(0)
View
View.getTop() - mList.getPaddingTop()
ListView
ListView
ListView.setSelectionFromTop()
ListView
.
MYYA
TA贡献1868条经验 获得超4个赞
Parcelable state;@Overridepublic void onPause() { // Save ListView state @ onPause Log.d(TAG, "saving listview state @ onPause"); state = listView.onSaveInstanceState(); super.onPause();}...@Overridepublic 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); }}
- 3 回答
- 0 关注
- 519 浏览
添加回答
举报
0/150
提交
取消