Android listView:scrollBy和touch事件

时间:2014-05-23 17:09:31

标签: android listview

我有ListView。我在构造函数中调用scrollBy(0,100)方法。当用户想要通过触摸事件列表滚动此列表时,返回到上一个(没有滚动)位置,之后滚动正常。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果要在重新创建片段(或Activity)时恢复ListView状态,最好使用ListViews的onSaveInstanceState()

// Save ListView state
Parcelable state = listView.onSaveInstanceState();

// Set new items
listView.setAdapter(adapter);

// Restore previous state (including selected item index and scroll position)
listView.onRestoreInstanceState(state);

如果只想以编程方式滚动ListView,请使用方法setSelectionFromTop(int position, int y)。 请参阅此问题的答案Maintain/Save/Restore scroll position when returning to a ListView