2011-01-10 100 views
5

朋友,如何在android listview中保持滾動條的位置?

我在ListView中顯示一個數組。

我改變動態按鈕點擊陣列的數據和呼叫

adapter.notifyDataSetInvalidated(); 

但不維護列表的滾動條位置。 (要列出的數據源的長度始終相同)。 任何人都可以指導我如何保持ListView的最後一個狀態?

任何幫助,將不勝感激。

回答

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); 
+0

請更正代碼或者這是不可理解的v在這裏?是否像(vfirst!= null) – UMAR 2011-01-10 11:56:43

3

如果您只想刷新數據的一些變化,使用

notifyDataSetChanged(); 

相反。滾動將處於相同的位置。