ActionMode.Callback


#1

In your example of creating a Contextual Action Bar you use an example in where the ListView’s choice mode is set to CHOICE_MODE_MULTIPLE_MODAL (enabled to select multiple items).
You then use ListView’s s​e​t​M​u​l​t​i​C​h​o​i​c​e​M​o​d​e​L​i​s​t​e​n​e​r​(…) method to implement the ActionMode.Callback methods.

I was wondering how/were to implement these ActionMode.Callback methods when your ListView is not in CHOICE_MODE_MULTIPLE_MODAL mode (or when not working with a ListView at all)?
If so, I assume you will then not call ListView’s s​e​t​M​u​l​t​i​C​h​o​i​c​e​M​o​d​e​L​i​s​t​e​n​e​r​(…) method.

If that is the case, where to implement the ActionMode.Callback methods and how to make sure they will be triggered?

Thanks in advance.


#2

CHOICE_MODE_MULTIPLE_MODAL and setMultiChoiceModeListener(…) work in tandem with one another. When multiple modal choice mode is selected, under the hood your ListView will call the startActionMode() method on your Activity, passing in the listener you set on setMultiChoiceModeLIstener as your action mode callback listener.

So if you want to use contextual action mode without multiple modal choice mode, or without a ListView at all, that’s your answer - create a listener that implements ActionMode.Callback and pass it in to startActionMode.


#3

I have tried to make a working example, maybe others find it interesting.
The example uses a setOnItemLongClickListener:

    [code]      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
		// Use FloatingContextMenus on old devices //
		registerForContextMenu(listView);
	} else{
		// Use Contextual ActionBar on new devices //
		
		// ListView in Single-Selection-Mode (is default?) //
		// Enable Long-Click //
		listView.setLongClickable(true);
		listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
		
			
			@Override
			public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
				
				getActivity().startActionMode(new ActionMode.Callback() {
					
					@Override
					public boolean onCreateActionMode(ActionMode mode, Menu menu) {
						MenuInflater inflater = mode.getMenuInflater();
						inflater.inflate(R.menu.crime_list_item_context, menu);
						return true;
					}
					
					@Override
					public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
						// Required but not used in this implementation
						return false;
					}
					
					@Override
					public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
						// Menu might have multiple items
						switch (item.getItemId()) {
						case R.id.menu_item_delete_crime:
							
							// Get both the ListView's adapter and the CrimeLab
							CrimeAdapter adapter = (CrimeAdapter)getListAdapter();
							CrimeLab crimeLab = CrimeLab.get(getActivity());
							
							// Walk through all items in ListView //
							for (int i = adapter.getCount() - 1; i >= 0; i--){
								// Determine if the item is 'Checked' //
								if (getListView().isItemChecked(i)){
									// If so: delete it from the CrimeLab //
									crimeLab.deleteCrime(adapter.getItem(i));
								}
							}
							
							// Prepare ActionMode to be destroyed //
							mode.finish();
							// Notify adapter to reload list //
							adapter.notifyDataSetChanged();
							
							
							return true;

						default:
							return false;
						}
						
					}
					
					@Override
					public void onDestroyActionMode(ActionMode mode) {
						// Required but not used in this implementation
						
					}
					
				});
				
				return true;
			}
		});
    }[/code]

#4

@Brabbeidas, I am trying this also. But how do you set an item as activated (pre honeycomb) in the list view so that the darker background will be shown once an item is selected for deletion?

I have my list view set to CHOICE_MODE_MULTIPLE, and in onclicklistener I set item checked if in action mode. However in 2.3.3 this does not automatically set the state as activated so that the background color changes to darker_gray. Setting item checked does automatically set the background to darker in later versions of android.