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); –