我有ListView
一个图像作为项目列表。现在我想在列表视图中执行拖放项目。因此,为此,我创建了一个扩展类“DragNDropListView” ListView
,并在该类中覆盖列表视图的 onTouch() 方法并执行拖放操作,它工作正常。
但我的要求是在我的列表视图中长按图像时执行此项目的拖放操作。那么如何做到这一点。我应该像在列表视图的方法中所做的那样做同样的事情,onTouch()
还是必须修改这个方法。
请帮我解决这个问题。
我的代码 onTouch 方法
@Override
public boolean onTouchEvent(MotionEvent ev)
{
final int action = ev.getAction();
final int x = (int) ev.getX();
final int y = (int) ev.getY();
if (action == MotionEvent.ACTION_DOWN)
{
mDragMode = true;
}
if (!mDragMode)
return super.onTouchEvent(ev);
switch (action)
{
case MotionEvent.ACTION_DOWN:
mStartPosition = pointToPosition(x,y);
if (mStartPosition != INVALID_POSITION)
{
int mItemPosition = mStartPosition - getFirstVisiblePosition();
mDragPointOffset = y - getChildAt(mItemPosition).getTop();
mDragPointOffset -= ((int)ev.getRawY()) - y;
startDrag(mItemPosition,y);
drag(0,y);// replace 0 with x if desired
}
break;
case MotionEvent.ACTION_MOVE:
{
drag(0,y);// replace 0 with x if desired
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
default:
mDragMode = false;
mEndPosition = pointToPosition(x,y);
stopDrag(mStartPosition - getFirstVisiblePosition());
if (mDropListener != null && mStartPosition != INVALID_POSITION && mEndPosition != INVALID_POSITION)
mDropListener.onDrop(mStartPosition, mEndPosition);
break;
}
return true;
}
// move the drag view
private void drag(int x, int y)
{
if (mDragView != null)
{
WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) mDragView.getLayoutParams();
layoutParams.x = x;
layoutParams.y = y - mDragPointOffset;
WindowManager mWindowManager = (WindowManager) getContext()
.getSystemService(Context.WINDOW_SERVICE);
mWindowManager.updateViewLayout(mDragView, layoutParams);
if (mDragListener != null)
mDragListener.onDrag(x, y, null);// change null to "this" when ready to use
}
}
// enable the drag view for dragging
private void startDrag(int itemIndex, int y)
{
stopDrag(itemIndex);
View item = getChildAt(itemIndex);
if (item == null) return;
item.setDrawingCacheEnabled(true);
if (mDragListener != null)
mDragListener.onStartDrag(item);
// Create a copy of the drawing cache so that it does not get recycled
// by the framework when the list tries to clean up memory
Bitmap bitmap = Bitmap.createBitmap(item.getDrawingCache());
WindowManager.LayoutParams mWindowParams = new WindowManager.LayoutParams();
mWindowParams.gravity = Gravity.TOP;
mWindowParams.x = 0;
mWindowParams.y = y - mDragPointOffset;
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
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
mWindowParams.format = PixelFormat.TRANSLUCENT;
mWindowParams.windowAnimations = 0;
Context context = getContext();
ImageView v = new ImageView(context);
v.setImageBitmap(bitmap);
WindowManager mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
mWindowManager.addView(v, mWindowParams);
mDragView = v;
}
// destroy drag view
private void stopDrag(int itemIndex)
{
if (mDragView != null) {
if (mDragListener != null)
mDragListener.onStopDrag(getChildAt(itemIndex));
mDragView.setVisibility(GONE);
WindowManager wm = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
wm.removeView(mDragView);
mDragView.setImageDrawable(null);
mDragView = null;
}
}
用于监听器
ListView listView = getListView();
if (listView instanceof DragNDropListView)
{
((DragNDropListView) listView).setDropListener(mDropListener);
((DragNDropListView) listView).setRemoveListener(mRemoveListener);
((DragNDropListView) listView).setDragListener(mDragListener);
}
private DropListener mDropListener = new DropListener()
{
public void onDrop(int from, int to)
{
ListAdapter adapter = getListAdapter();
if (adapter instanceof ListViewAdapter)
{
((ListViewAdapter)adapter).onDrop(from, to);
getListView().invalidateViews();
}
}
};
private RemoveListener mRemoveListener = new RemoveListener()
{
public void onRemove(int which)
{
ListAdapter adapter = getListAdapter();
if (adapter instanceof ListViewAdapter) {
((ListViewAdapter)adapter).onRemove(which);
getListView().invalidateViews();
}
}
};
private DragListener mDragListener = new DragListener()
{
public void onDrag(int x, int y, ListView listView)
{
}
public void onStartDrag(View itemView)
{
itemView.setVisibility(View.INVISIBLE);
ImageView iv = (ImageView)itemView.findViewById(R.id.image);
if (iv != null) iv.setVisibility(View.VISIBLE);
}
public void onStopDrag(View itemView)
{
itemView.setVisibility(View.VISIBLE);
ImageView iv = (ImageView)itemView.findViewById(R.id.image);
if (iv != null) iv.setVisibility(View.VISIBLE);
}
};