Try a different approach using WindowManager:
package com.example.cooldraganddrop; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.PixelFormat; import android.graphics.drawable.BitmapDrawable; import android.util.AttributeSet; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.ImageView; public class CoolDragAndDropGridView extends SpanVariableGridView implements `View.OnTouchListener {` private static final int ITEM_HOVER_DELAY = 450; private int mDragPointX; private int mDragPointY; private int mDragOffsetX; private int mDragOffsetY; private int mDragPosition = AdapterView.INVALID_POSITION; private int mDropPosition = AdapterView.INVALID_POSITION; private int mCurrentPosition = AdapterView.INVALID_POSITION; private Runnable mDelayedOnDragRunnable = null; ScrollingStrategy mScrollingStrategy = null; WindowManager mWindowManager = null; WindowManager.LayoutParams mWindowParams = null; private ImageView mDragImageView = null; private boolean mDragAndDropStarted = false; private DragAndDropListener mDragAndDropListener = null; private OnTrackTouchEventsListener mOnTrackTouchEventsListener = null; public static interface OnTrackTouchEventsListener { void trackTouchEvents(final MotionEvent motionEvent); }; public static interface DragAndDropListener { void onDragItem(int from); void onDraggingItem(int from, int to); void onDropItem(int from, int to); boolean isDragAndDropEnabled(int position); } public CoolDragAndDropGridView(Context context) { super(context); initialize(); } public CoolDragAndDropGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initialize(); } public CoolDragAndDropGridView(Context context, AttributeSet attrs) { super(context, attrs); initialize(); } private void initialize() { setOnTouchListener(this); setChildrenDrawingOrderEnabled(true); } public void startDragAndDrop() { mDragAndDropStarted = true; } public void setDragAndDropListener(DragAndDropListener dragAndDropListener) { mDragAndDropListener = dragAndDropListener; } private void destroyDragImageView() { if (mDragImageView != null) { mWindowManager.removeView(mDragImageView); BitmapDrawable bitmapDrawable = (BitmapDrawable) mDragImageView.getDrawable(); if (bitmapDrawable != null) { final Bitmap bitmap = bitmapDrawable.getBitmap(); if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); } } mDragImageView.setImageDrawable(null); mDragImageView = null; } } private ImageView createDragImageView(final View v, final int x, final int y) { v.destroyDrawingCache(); v.setDrawingCacheEnabled(true); Bitmap bm = Bitmap.createBitmap(v.getDrawingCache()); mDragPointX = x - v.getLeft(); mDragPointY = y - v.getTop(); mWindowParams = new WindowManager.LayoutParams(); mWindowParams.gravity = Gravity.TOP | Gravity.LEFT; mWindowParams.x = x - mDragPointX + mDragOffsetX; mWindowParams.y = y - mDragPointY + mDragOffsetY; mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT; mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT; mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; mWindowParams.format = PixelFormat.TRANSLUCENT; mWindowParams.alpha = 0.7f; mWindowParams.windowAnimations = 0; ImageView iv = new ImageView(getContext()); iv.setBackgroundColor(Color.parseColor("#ff555555")); iv.setImageBitmap(bm); mWindowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
An example using this drag method here
user3074632
source share