Source:
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { if (actionMode != null) { return false; } historyEntryList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); actionMode = ((AppCompatActivity)getActivity()).startSupportActionMode(new ActionMode.Callback() { private final String actionModeTag = "actionModeHistory"; @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { mode.getMenuInflater().inflate(R.menu.menu_history_context, menu); setActionModeIntTitle(historyEntryList.getCheckedItemCount() + 1); return true; } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { mode.setTag(actionModeTag); int count = historyEntryList.getCheckedItemCount(); if (actionMode != null) { if (count == 0) { mode.finish(); } else { setActionModeIntTitle(count); } } return false; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { if (item.getItemId() == R.id.menu_delete_selected_history) { SparseBooleanArray checkedItems = historyEntryList.getCheckedItemPositions(); for (int i = 0; i < checkedItems.size(); i++) { if (checkedItems.valueAt(i)) { app.getDatabaseClient(HistoryEntry.class).delete( HistoryEntry.DATABASE_TABLE.fromCursor((Cursor) adapter.getItem(checkedItems.keyAt(i))), PageHistoryContract.PageWithImage.SELECTION); } } if (checkedItems.size() == historyEntryList.getAdapter().getCount()) { entryFilter.setVisibility(View.GONE); } mode.finish(); return true; } else { throw new RuntimeException("Unknown context menu item clicked"); } } @Override public void onDestroyActionMode(ActionMode mode) { historyEntryList.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); actionMode = null; historyEntryList.clearChoices(); historyEntryList.requestLayout(); } }); historyEntryList.setItemChecked(position, true); return true; }

Target:
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { if (actionMode != null) { return false; } historyEntryList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); actionMode = ((AppCompatActivity)getActivity()).startSupportActionMode(new ActionMode.Callback() { private final String actionModeTag = "actionModeHistory"; @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { mode.getMenuInflater().inflate(R.menu.menu_history_context, menu); setActionModeIntTitle(historyEntryList.getCheckedItemCount() + 1, mode); return true; } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { mode.setTag(actionModeTag); int count = historyEntryList.getCheckedItemCount(); if (actionMode != null) { if (count == 0) { mode.finish(); } else { setActionModeIntTitle(count, mode); } } return false; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { if (item.getItemId() == R.id.menu_delete_selected_history) { SparseBooleanArray checkedItems = historyEntryList.getCheckedItemPositions(); for (int i = 0; i < checkedItems.size(); i++) { if (checkedItems.valueAt(i)) { app.getDatabaseClient(HistoryEntry.class).delete( HistoryEntry.DATABASE_TABLE.fromCursor((Cursor) adapter.getItem(checkedItems.keyAt(i))), PageHistoryContract.PageWithImage.SELECTION); } } if (checkedItems.size() == historyEntryList.getAdapter().getCount()) { entryFilter.setVisibility(View.GONE); } mode.finish(); return true; } else { throw new RuntimeException("Unknown context menu item clicked"); } } @Override public void onDestroyActionMode(ActionMode mode) { historyEntryList.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); actionMode = null; historyEntryList.clearChoices(); historyEntryList.requestLayout(); } }); historyEntryList.setItemChecked(position, true); return true; }