简体   繁体   English

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

[英]how to maintain scroll bar position in android listview?

Friends, 朋友们,

I am displaying an array in ListView. 我在ListView中显示一个数组。

I change data of array dynamically on button click and call 我在按钮点击和调用时动态更改数组的数据

adapter.notifyDataSetInvalidated();

It does not maintain scroll bar position of list. 它不保持列表的滚动条位置。 (the lenght of data source to list is always same). (列表中数据源的长度始终相同)。 Could any one guide me how to keep the last state of ListView ? 任何人都可以指导我如何保持ListView的最后状态吗?

Any help would be appreciated. 任何帮助,将不胜感激。

Try this: 试试这个:

//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);

If you only want to refresh some changes in data, use 如果您只想刷新数据中的某些更改,请使用

notifyDataSetChanged();

Instead. 代替。 And scroll will be in the same position. 滚动将处于相同的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM