社區! 我需要幫助更新ListView中的不可見項目。這不是關於項目內容,而是項目的視圖表示。好吧,讓我給你看我的例子。我有一個字符串數組:如何在ListView中更新某些不可見項目的視圖? [Android]
<string-array name="reminder_notifications">
<item>15 minutes before</item>
<item>30 minutes before</item>
<item>1 hour before</item>
<item>1.5 hour before</item>
<item>5 hours before</item>
<item>8 hours before</item>
<item>1 day before</item>
</string-array>
在活動我創建適配器:
adapterNotifications = ArrayAdapter.createFromResource(this, R.array.reminder_notifications, R.layout.dialog_list_multiple_choise);
後,用一些方法我計算whitch項目從字符串數組是avaliable當前提醒。例如。如果用戶在16:00設置提醒爲16:45,那麼他只能選擇項目15 minutes before
和30 minutes before
。其他項目應該禁用。 所以,谷歌後,我發現瞭如何獲得在特定位置獲得無形的ListView孩子:
public View getViewByPosition(int position, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (position < firstListItemPosition || position > lastListItemPosition) {
return listView.getAdapter().getView(position, listView.getChildAt(position), listView);
} else {
final int childIndex = position - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
現在,我面臨着最後一個問題(我希望) - 如何更新項目的看法,我從得到上面的方法?我試圖用這樣的:
View v = getViewByPosition(position, lvNotifications);
v.setEnabled(true);
但它只有在第一次打開的對話框中查看更新,換句話說,我必須與ListView控件打開對話窗口,關閉並重新打開。只有在這種情況下,我才能看到更新的視圖。 我知道,我的英語很糟糕,所以有以下屏幕截圖:
謝謝。
男人,你真棒。謝謝!並非常感謝您的解釋!告訴我什麼是更好的CheckedTextView或TextView + CheckBox? –
CheckedTextView旨在與ListView和setMultipleChoiceItems(...)以及setSingleChoiceItems(...)配合使用,而不是像我的解決方案中那樣單獨使用或作爲普通視圖使用。使用TextView和RelativeLayout,完成後,這也支持RTL佈局,並且沒有視覺差異。查看我在回答結尾處鏈接的GitHub倉庫中使用的佈局。 我對我的解決方案做了一些細微的更改(用BaseAdapter替換了ArrayAdapter,更改了一些填充) – crysxd