Skip to content

5.x | Selection Modes

Davide Steduto edited this page Jun 25, 2016 · 18 revisions

In this page

  • Introduction
  • Simple selection (IDLE)
  • Single selection (SINGLE)
  • Multi selection (MULTI)
  • Handling rotation

Introduction

In this page all selection is managed without ActionModeHelper.
Go to the page ActionModeHelper for the new way to handle selection.

Simple selection (IDLE)

Mode IDLE is the default value at the start up. However to switch from another mode, write the following statement:

mAdapter.setMode(SelectableAdapter.MODE_IDLE);

Single selection (SINGLE)

In your Activity/Fragment creation set the Mode SINGLE.

mAdapter.setMode(SelectableAdapter.MODE_SINGLE);

In onItemClick(), call toggleSeletion() to register the selection on that position (it won't trigger notifyItemChanged()) and return true for the itemView activation:

@Override
public boolean onItemClick(int position) {
	if (position != mActivatedPosition) setActivatedPosition(position);
	return true;//Important!
}
//Optional
//To evaluate if setActivatedPosition can be included in the library
private void setActivatedPosition(int position) {
	mActivatedPosition = position;
	mAdapter.toggleSelection(position);//Important!
}

Note: With version 5.x the ViewHolder automatically handles the view activation. Check the wiki page ViewHolders for more details.
Optionally, you can now set View Elevation for the activation, API compatibility is maintained.

/**
 * Allows to set elevation while the view is activated.
 * <p>Override to return desired value of elevation on this itemView.</p>
 *
 * @return never elevate, returns 0dp if not overridden
 */
public float getActivationElevation() {
	return 0f;
}

Note: Also the binding of the selection status (when user scrolls) is automatically handled by the Adapter, you don't have to call any view activation. This is an extract of the pre-implemented onBindViewHolder():

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List payloads) {
	//When user scrolls, this line binds the correct selection status
	holder.itemView.setActivated(isSelected(position));
	...
}

Multi selection (MULTI)

From Android suggestion I decided to use view Activation state instead of view Selection state as others libraries mistakenly apply:

A view can be activated or not. Note that activation is not the same as selection. Selection is a transient property, representing the view (hierarchy) the user is currently interacting with.
Activation is a longer-term state that the user can move views in and out of. For example, in a list view with single or multiple selection enabled, the views in the current selection set are activated. (Um, yeah, we are deeply sorry about the terminology here.) The activated state is propagated down to children of the view it is set on.

To be continued...

Handling rotation

Preserve the current selection state, together with other flags:

@Override
public void onSaveInstanceState(Bundle outState) {
	mAdapter.onSaveInstanceState(outState);
	super.onSaveInstanceState(outState);
}

Restore the previous selection state:

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
	super.onRestoreInstanceState(savedInstanceState);
	//Restore previous state
	if (savedInstanceState != null) {
		//Selection
		mAdapter.onRestoreInstanceState(savedInstanceState);
		if (mAdapter.getSelectedItemCount() > 0) {
			mActionMode = startSupportActionMode(this);
			setContextTitle(mAdapter.getSelectedItemCount());
		}
	}
}
Clone this wiki locally