如何在android listview中保持滚动条位置?

时间:2011-01-10 09:54:57

标签: android listview

我在ListView中显示一个数组。

我在点击按钮时动态更改数组数据并调用

adapter.notifyDataSetInvalidated();

它不保持列表的滚动条位置。 (列表中数据源的长度始终相同)。 任何人都可以指导我如何保持ListView的最后状态吗?

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:10)

试试这个:

//Get the top position from the first visible element
int idx = list.getFirstVisiblePosition();
View vfirst = list.getChildAt(0);
int pos = 0;
if (vfirst != null) pos = vfirst.getTop();

//Restore the position
list.setSelectionFromTop(idx, pos);

答案 1 :(得分:3)

如果您只想刷新数据中的某些更改,请使用

notifyDataSetChanged();

相反。滚动将处于相同的位置。

相关问题