8
我已經創建了一個動態的ListView,其中的對象是從頂部添加的。Android的ListView setSelectionFromTop不工作
當用戶按下按鈕時,listView將從數組內容更新,然後在自定義arrayAdapter上調用notifyDataSetChanged()。
現在我想十個分量增加時,列表中的位置,所以我加了這段代碼:
// pausedCounter trace the number of objects(lines) to add to the listView
int idx = listView.getFirstVisiblePosition() + pausedCounter;
View first = listView.getChildAt(0);
int position = 0;
if (first != null)
position = first.getTop();
// cycle to add the new objects to the listView
for (Tweet[] tweets1 : pausedTweets)
super.updateTweets(tweets1);
listView.setSelectionFromTop(idx, position);
// reset of counter and accumulator
pausedTweets = new ArrayList<Tweet[]>();
pausedCounter = 0;
此代碼的行爲以這種方式:如果getFirstVisiblePosition返回2,pausedCounter是5,更新後該列表將被設置爲新的五個元素的第三個。
我想要的是將列表中的第一個可見元素設置爲第8個。
經過進一步的測試中,我發現,列表視圖的兒童數量這段代碼的運行過程中不發生變化,所以它我叫setSelectionFromTop後更新列表視圖的大小。可能是這個問題?
非常感謝,與此:listview.post(...)也listView.setSelection再次爲我工作(沒有任何其他方式工作) – cV2