私は、オブジェクトが上から追加される動的ListViewを作成しました。Android ListView setSelectionFromTop not working
ユーザーがボタンを押すと、配列の内容からlistViewが更新され、カスタムarrayAdapterでnotifyDataSetChanged()が呼び出されます。
は今、私は追加するリスト位置をmantainしたいので、私はこのコードを追加:
// 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である場合リストは新しい5つの要素の3番目に設定されます。
私が望むのは、リストの最初の可視要素を8番目に設定することです。
はさらなる試験の後、私は、ListViewコントロールの子供の数は、コードのこの部分の実行中に変化しないことが分かったので、それは私がsetSelectionFromTopと呼ば後のListView のサイズを更新します。これが問題だろうか?変更位置の前にリストビューの更新を待つようにポストメソッド許可を使用して
listView.post(new Runnable() {
@Override
public void run() {
listView.setSelectionFromTop(idx, finalPosition);
}
});
:
ありがとう:listview.post(...)listView.setSelectionも私のために働いた(他の方法では動作しなかった) – cV2