public class

ItemDragDetector

extends Object
implements RecyclerView.OnItemTouchListener
java.lang.Object
   ↳ com.shinobicontrols.grids.supplement.ItemDragDetector

Class Overview

An implementation of RecyclerView.OnItemTouchListener that responds to long press and drag gestures made on items within the grid. It is given an ItemDragDetector.OnItemDragListener which determines what actions should be taken at each stage of the drag gesture (long press, move, release).

This listener will swallow the touch event when the start of a drag gesture has been recognized (i.e. after a long press). This means if the Views for the items covered by the drag gesture have their own View.OnClickListeners they will not be called.

Summary

Nested Classes
interface ItemDragDetector.OnItemDragListener

Callback to be invoked when an item in the grid has been long pressed and dragged. 

Public Constructors
ItemDragDetector(Context context)

Creates a new ItemDragDetector.

Public Methods
final void addOnItemDragListener(ItemDragDetector.OnItemDragListener onItemDragListener)

Adds an ItemDragDetector.OnItemDragListener to be invoked when an item in the grid has been dragged.

final int getItemPosition(RecyclerView recyclerView, float x, float y)

Returns the position of the item in recyclerView at the given X and Y co-ordinates.

final void removeOnItemDragListener(ItemDragDetector.OnItemDragListener onItemDragListener)

Removes the given ItemDragDetector.OnItemDragListener from this ItemDragDetector's list of listeners to be invoked when an item in the grid has been dragged.

final void setChildViewFinder(ChildViewFinder childViewFinder)

Sets the ChildViewFinder for this ItemDragDetector.

[Expand]
Inherited Methods
From class java.lang.Object
From interface android.support.v7.widget.RecyclerView.OnItemTouchListener

Public Constructors

public ItemDragDetector (Context context)

Creates a new ItemDragDetector. A Context is required for construction.

Parameters
context the application's Context

Public Methods

public final void addOnItemDragListener (ItemDragDetector.OnItemDragListener onItemDragListener)

Adds an ItemDragDetector.OnItemDragListener to be invoked when an item in the grid has been dragged. Passing in null will throw an IllegalArgumentException.

Parameters
onItemDragListener the ItemDragDetector.OnItemDragListener to be added to the list of listeners to be invoked when a drag gesture is performed on an item in the grid, cannot be null

public final int getItemPosition (RecyclerView recyclerView, float x, float y)

Returns the position of the item in recyclerView at the given X and Y co-ordinates. If no child View can be found at these co-ordinates a position of -1 is returned.

Parameters
recyclerView the RecyclerView in which to search for a child View for the given X and Y co-ordinates
x the X co-ordinate, relative to recyclerView at which to search for an item
y the Y co-ordinate, relative to recyclerView at which to search for an item
Returns
  • the position of the item in recyclerView that is represented by the child View at the X and Y co-ordinates of the MotionEvent

public final void removeOnItemDragListener (ItemDragDetector.OnItemDragListener onItemDragListener)

Removes the given ItemDragDetector.OnItemDragListener from this ItemDragDetector's list of listeners to be invoked when an item in the grid has been dragged.

Parameters
onItemDragListener the ItemDragDetector.OnItemDragListener to be removed from the list of listeners to be invoked when a drag gesture is performed on an item in the grid

public final void setChildViewFinder (ChildViewFinder childViewFinder)

Sets the ChildViewFinder for this ItemDragDetector. This defines the way child views are searched for. For example, you may want to find the nearest View to a given X and Y co-ordinate thus including any space for gridlines into this calculation.

Parameters
childViewFinder the ChildViewFinder this ItemDragDetector should use