public abstract class SelectableAdapter extends RecyclerView.Adapter implements FastScroller.BubbleTextCreator, FastScroller.OnScrollStateChangeListener, FastScroller.AdapterInterface
Also it manages the FastScroller.
This class is extended byAnimatorAdapter.FlexibleAdapter,
AnimatorAdapter| Modifier and Type | Class and Description |
|---|---|
static interface |
SelectableAdapter.Mode
Annotation interface for selection modes:
SelectableAdapter.Mode.IDLE, SelectableAdapter.Mode.SINGLE, SelectableAdapter.Mode.MULTI |
| Modifier and Type | Field and Description |
|---|---|
protected boolean |
isFastScroll
Flag when fast scrolling is active.
|
protected FastScroller.Delegate |
mFastScrollerDelegate |
protected boolean |
mLastItemInActionMode
ActionMode selection flag LastItemInActionMode.
|
protected RecyclerView |
mRecyclerView |
protected boolean |
mSelectAll
ActionMode selection flag SelectAll.
|
| Constructor and Description |
|---|
SelectableAdapter() |
| Modifier and Type | Method and Description |
|---|---|
boolean |
addSelection(int position)
Adds the selection status for the given position without notifying the change.
|
void |
clearSelection()
Clears the selection status for all items one by one and it doesn't stop animations in the items.
|
static void |
enableLogs(int level)
Call this once, to enable or disable internal logs with custom level.
|
java.util.Set<FlexibleViewHolder> |
getAllBoundViewHolders()
Usually
RecyclerView binds 3 items more than the visible items. |
FastScroller |
getFastScroller() |
IFlexibleLayoutManager |
getFlexibleLayoutManager()
Current instance of the wrapper class for LayoutManager suitable for FlexibleAdapter.
|
int |
getMode()
The current selection mode of the Adapter.
|
RecyclerView |
getRecyclerView() |
int |
getSelectedItemCount()
Counts the selected items.
|
java.util.List<java.lang.Integer> |
getSelectedPositions()
Retrieves the list of selected items.
|
java.util.Set<java.lang.Integer> |
getSelectedPositionsAsSet()
Retrieves the set of selected items.
|
boolean |
isFastScrollerEnabled() |
boolean |
isLastItemInActionMode() |
abstract boolean |
isSelectable(int position)
Checks if the current item has the property
selectable = true. |
boolean |
isSelectAll() |
boolean |
isSelected(int position)
Indicates if the item, at the provided position, is selected.
|
void |
onAttachedToRecyclerView(RecyclerView recyclerView)
Attaches the
FastScrollerDelegate to the RecyclerView if necessary. |
void |
onBindViewHolder(RecyclerView.ViewHolder holder,
int position,
java.util.List payloads) |
java.lang.String |
onCreateBubbleText(int position) |
void |
onDetachedFromRecyclerView(RecyclerView recyclerView)
Detaches the
FastScrollerDelegate from the RecyclerView if necessary. |
void |
onFastScrollerStateChange(boolean scrolling)
Triggered when FastScroller State is changed.
|
void |
onRestoreInstanceState(android.os.Bundle savedInstanceState)
Restores the previous state of the selection on the items.
|
void |
onSaveInstanceState(android.os.Bundle outState)
Saves the state of the current selection on the items.
|
void |
onViewRecycled(RecyclerView.ViewHolder holder) |
boolean |
removeSelection(int position)
Removes the selection status for the given position without notifying the change.
|
void |
selectAll(java.lang.Integer... viewTypes)
Sets the selection status for all items which the ViewTypes are included in the specified array.
|
void |
setFastScroller(FastScroller fastScroller)
Sets up the
FastScroller with automatic fetch of accent color. |
void |
setFlexibleLayoutManager(IFlexibleLayoutManager flexibleLayoutManager)
Allow to use a custom LayoutManager.
|
void |
setMode(int mode)
Sets the mode of the selection:
SelectableAdapter.Mode.IDLE Default. |
protected void |
swapSelection(int fromPosition,
int toPosition)
Helper method to easily swap selection between 2 positions only if one of the positions
is not selected.
|
void |
toggleFastScroller()
Displays or Hides the
FastScroller if previously configured. |
void |
toggleSelection(int position)
Toggles the selection status of the item at a given position.
|
static void |
useTag(java.lang.String tag) |
protected RecyclerView mRecyclerView
protected FastScroller.Delegate mFastScrollerDelegate
protected boolean isFastScroll
Used to know if user is fast scrolling.
protected boolean mSelectAll
Used when user click on selectAll action button in ActionMode.
protected boolean mLastItemInActionMode
Used when user returns to SelectableAdapter.Mode.IDLE and no selection is active.
public static void enableLogs(int level)
level - One of Log.Level valuepublic static void useTag(java.lang.String tag)
public void onAttachedToRecyclerView(RecyclerView recyclerView)
Attaches the FastScrollerDelegate to the RecyclerView if necessary.
public void onDetachedFromRecyclerView(RecyclerView recyclerView)
Detaches the FastScrollerDelegate from the RecyclerView if necessary.
public RecyclerView getRecyclerView()
public IFlexibleLayoutManager getFlexibleLayoutManager()
return wrapper class for any non-conventional LayoutManagers or null if not initialized.
public void setFlexibleLayoutManager(IFlexibleLayoutManager flexibleLayoutManager)
flexibleLayoutManager - the custom LayoutManager suitable for FlexibleAdapterpublic void setMode(int mode)
SelectableAdapter.Mode.IDLE Default. Configures the adapter so that no item can be selected;
SelectableAdapter.Mode.SINGLE configures the adapter to react at the single tap over an item
(previous selection is cleared automatically);
SelectableAdapter.Mode.MULTI configures the adapter to save the position to the list of the
selected items.
mode - one of SelectableAdapter.Mode.IDLE, SelectableAdapter.Mode.SINGLE, SelectableAdapter.Mode.MULTIpublic int getMode()
SelectableAdapter.Mode.IDLE,
SelectableAdapter.Mode.SINGLE,
SelectableAdapter.Mode.MULTIpublic boolean isSelectAll()
public boolean isLastItemInActionMode()
SelectableAdapter.Mode.IDLE or SelectableAdapter.Mode.SINGLE and no
selection is active, false otherwisepublic boolean isSelected(int position)
position - Position of the item to check.public abstract boolean isSelectable(int position)
selectable = true.position - the current position of the item to checkpublic void toggleSelection(int position)
The behaviour depends on the selection mode previously set with setMode(int).
FlexibleViewHolder.toggleActivation() called in onClick event
Usage:
DrawableUtils or via layout's item:
android:background="?attr/selectableItemBackground", pointing to a custom Drawable
in the style.xml (note: prefix ?android:attr doesn't work).position - Position of the item to toggle the selection status for.public final boolean addSelection(int position)
position - Position of the item to add the selection status for.isSelectable(int)public final boolean removeSelection(int position)
position - Position of the item to remove the selection status for.protected void swapSelection(int fromPosition,
int toPosition)
fromPosition - first positiontoPosition - second positionpublic void selectAll(java.lang.Integer... viewTypes)
viewTypes - The ViewTypes for which we want the selection, pass nothing to select allpublic void clearSelection()
Note 1: Items are not rebound, so an eventual animation is not stopped!
Note 2: This method use java.util.Iterator on synchronized collection to
avoid java.util.ConcurrentModificationException.
public void onBindViewHolder(RecyclerView.ViewHolder holder,
int position,
java.util.List payloads)
public void onViewRecycled(RecyclerView.ViewHolder holder)
public java.util.Set<FlexibleViewHolder> getAllBoundViewHolders()
RecyclerView binds 3 items more than the visible items.public int getSelectedItemCount()
public java.util.List<java.lang.Integer> getSelectedPositions()
The list is a copy and it's sorted.
public java.util.Set<java.lang.Integer> getSelectedPositionsAsSet()
The set is sorted.
public void onSaveInstanceState(android.os.Bundle outState)
outState - Current statepublic void onRestoreInstanceState(android.os.Bundle savedInstanceState)
savedInstanceState - Previous statepublic void toggleFastScroller()
FastScroller if previously configured.
setFastScroller(FastScroller)public boolean isFastScrollerEnabled()
FastScroller is configured and shown, false otherwisepublic FastScroller getFastScroller()
FastScroller objectpublic void setFastScroller(FastScroller fastScroller)
FastScroller with automatic fetch of accent color.
IMPORTANT: Call this method after the adapter is added to the RecyclerView.
NOTE: If the device has at least Lollipop, the Accent color is fetched, otherwise for previous version, the default value is used.setFastScroller in interface FastScroller.AdapterInterfacefastScroller - instance of FastScrollerpublic java.lang.String onCreateBubbleText(int position)
onCreateBubbleText in interface FastScroller.BubbleTextCreatorposition - the position of the handlepublic void onFastScrollerStateChange(boolean scrolling)
onFastScrollerStateChange in interface FastScroller.OnScrollStateChangeListenerscrolling - true if the user is actively scrolling, false when idle