Example usage for android.view DragEvent ACTION_DROP

List of usage examples for android.view DragEvent ACTION_DROP

Introduction

In this page you can find the example usage for android.view DragEvent ACTION_DROP.

Prototype

int ACTION_DROP

To view the source code for android.view DragEvent ACTION_DROP.

Click Source Link

Document

Action constant returned by #getAction() : Signals to a View that the user has released the drag shadow, and the drag point is within the bounding box of the View and not within a descendant view that can accept the data.

Usage

From source file:app.umitems.greenclock.widget.sgv.StaggeredGridView.java

@Override
public boolean onDragEvent(DragEvent ev) {
    if (!isDragReorderingSupported()) {
        // If the consumer of this StaggeredGridView has not registered a ReorderListener,
        // don't bother handling drag events.
        return false;
    }// w w  w.java  2  s  .com

    final int x = (int) ev.getX();
    final int y = (int) ev.getY();

    switch (ev.getAction()) {
    case DragEvent.ACTION_DRAG_LOCATION:
        if (mDragState == ReorderUtils.DRAG_STATE_DRAGGING) {
            handleDrag(x, y);
            mLastTouchY = y;
        }

        // Kick off the scroll handler on the first drag location event,
        // if it's not already running
        if (!mIsDragScrollerRunning &&
        // And if the distance traveled while dragging exceeds the touch slop
                ((Math.abs(x - mTouchDownForDragStartX) >= 4 * mTouchSlop)
                        || (Math.abs(y - mTouchDownForDragStartY) >= 4 * mTouchSlop))) {
            // Set true because that the scroller is running now
            mIsDragScrollerRunning = true;

            if (mScrollHandler == null) {
                mScrollHandler = getHandler();
            }
            mScrollHandler.postDelayed(mDragScroller, SCROLL_HANDLER_DELAY);
        }

        return true;

    case DragEvent.ACTION_DROP:
    case DragEvent.ACTION_DRAG_ENDED:
        // We can either expect to receive:
        // 1. Both {@link DragEvent#ACTION_DROP} and then
        //    {@link DragEvent#ACTION_DRAG_ENDED} if the drop is over this view.
        // 2. Only {@link DragEvent#ACTION_DRAG_ENDED} if the drop happened over a
        //    different view.
        // For this reason, we should always handle the drop. In case #1, if this code path
        // gets executed again then nothing will happen because we will have already
        // updated {@link #mDragState} to not be {@link ReorderUtils#DRAG_STATE_DRAGGING}.
        if (mScrollHandler != null) {
            mScrollHandler.removeCallbacks(mDragScroller);
            // Scroller is no longer running
            mIsDragScrollerRunning = false;
        }

        return true;
    }

    return false;
}