0
在我的android應用程序中,有從遊標(db)獲取的listview。它具有啓用多項選擇和actionbarsherlock的動作模式,並對所選項目執行操作。android listview with actionmode
我停留在這種情況下,
我選擇列表視圖兩個項目和行動模式說選擇兩項。現在,如果我點擊後,動作模式結束,但checkeditem保持選中狀態。記住,listview.getChildCount()只會返回可見的項目,如何取消選中所有的後退鍵?
private static SparseBooleanArray checkedItemsArray;
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3)
{
boolean isOneRowChecked = false;
CheckableRelativeLayout r;
checkedItemsArray = listView.getCheckedItemPositions();
for (int i = 0; i < listView.getChildCount(); i++)
{
r = (CheckableRelativeLayout) listView.getChildAt(i);
if (r != null && r.isChecked())
{
r.setBackgroundColor(Color.DKGRAY);
isOneRowChecked = true;
} else
{
r.setBackgroundColor(Color.BLACK);
}
}
mActionMode = OtherActivity.this.startActionMode(mActionModeCallback);
if (!isOneRowChecked && mActionMode != null)
{
mActionMode.finish();
checkedItemsArray = null;
}
}
我的活動也正在實施OnScrollListener,所以
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
int i = firstVisibleItem;
if (checkedItemsArray != null)
{
for (int k = 0; k < visibleItemCount; k++)
{
CheckableRelativeLayout r = (CheckableRelativeLayout) listView.getChildAt(k);
if (checkedItemsArray.get(i))
{
r.setChecked(true);
r.setBackgroundColor(Color.DKGRAY);
} else
{
r.setChecked(false);
r.setBackgroundColor(Color.BLACK);
}
i++;
}
}
else
{
for (int k = 0; k < visibleItemCount; k++)
{
listView.setItemChecked(k, false);
CheckableRelativeLayout r = (CheckableRelativeLayout) listView.getChildAt(k);
r.setChecked(false);
r.setBackgroundColor(Color.BLACK);
}
}
}
但onDestroyActionMode被稱爲每次當列表視圖中選擇更改? – anonymous
onDestroyActionMode在動作模式即將退出並銷燬時調用,因此只要確保在您調用的onCreateActionMode中調用了listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); –