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

UMA*_*MAR 5 android listview

朋友们,

我在ListView中显示一个数组.

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

adapter.notifyDataSetInvalidated();
Run Code Online (Sandbox Code Playgroud)

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

任何帮助,将不胜感激.

Fra*_*ita 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);
Run Code Online (Sandbox Code Playgroud)

  • 代码中的微小错误:`v.getTop()`应该是`vfirst.getTop()` (2认同)