Question

To use contextual action bar on Android Honeycomb and later, I would normally check for the SDK_INT being 11 or later, and then proceed with mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

Now, with ActionBarSherlock I am trying to port this to older devices. Now I use CHOICE_MODE_MULTIPLE instead of CHOICE_MODE_MULTIPLE_MODAL, and inside the OnItemLongClickListener, I manage to call getSherlockActivity().startActionMode(myActionModeCallback);

The thing is that, when first I long press an item in the ListView, the contextual action bar correctly shows up.

But when I click on another ListView item, the item gets selected, and right after that its click listener gets fired. (I use that listener, so that when the user clicks on an item, it opens up a new activity where the user can edit the item, so in this case the item gets selected and immediately this new activity gets started, and ruins my contextual action bar)

It shouldn't be happening but I suppose I'm doing something wrong. I've thought about setting the OnListItemClick listener to null until I call ActionMode.finish() where I could revert it back to the previous listener, but I believe there must be a cleaner way to do it.

Was it helpful?

Solution

As far as I can understand your question, here's a solution for you.

You can first check if there are selected items like

boolean hasCheckedItems = yourListAdapter.getSelectedCount() > 0;

Then you can check for the following conditions

if (hasCheckedItems && ActionModeObject == null)
//there are some selected items, start the ActionMode 

else if (!hasCheckedItems && ActionModeObject != null)
// there no selected items, finish the ActionMode
ActionModeObject.finish();

if (ActionModeObject != null)
//Action Button Clicked
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top