I am using setMultiChoiceModeListener
on Listview
to invoke Action Mode for selecting multiple items to be deleted. On ListView
scroll I load more data from server and notify the adapter. After adapter
has been notified, the Action Mode (if invoked) is destroyed and recreated, which makes the title and selected arraylist empty (You can see the picture below). While the listview item selection remains. I want the CAB to be persistent like we see it in Gmail app, where it does not destroy on loading more data.
Below is the code for Action Mode
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
Item mItem = adapter.items.get(position);
if (mItem.isSection()) {
} else {
mode.setTitle(listView.getCheckedItemCount() + " Selected");
EntryItem mEntryItem = (EntryItem) mItem;
orderid = mEntryItem.orderId;
if (checked) {
selectedIdList.add(orderid);
} else {
selectedIdList.remove(orderid);
}
// Toggle the state of item after every click on it
adapter.toggleSelection(position);
}
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getItemId() == R.id.delete){
if (selectedIdList.size() > 0) {
deleteItems(selectedIdList);
}
mode.finish();
return true;
}
return false;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.menu_main, menu);
actionMode = mode;
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
actionMode = null;
if (selectedIdList.size() > 0) {
selectedIdList.clear();
adapter.mSelectedItemsIds.clear();
}
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
});
Any help will be highly appreciated, thanks in advance.
mode.finish()
inonActionItemClicked
is called on tap of delete icon inactionbar
, otherwise it will not be called there. – Comprador