How to implement drag and drop in ListView to another Listview?

I am implementing Drag and Drop from a ListView to another ListView, I have an example application,

How to drag a Listview item to another view list

using the above application, one application was created, it works fine up to the 4th ListView image. In my opinion By default, ListView displays up to 3rd position (meaning 0,1,2,3) if I drag the second position from the first ListView and drop in the second ListView. It displays the same image. When I scroll down, positions 4,5,6,7. If I dragged the 6th position, it takes the 2nd position. Please help me

+4
source share
1 answer

You should use a for loop. Then specify the number counter in the list and check each count. Check if the cycle IDs are the same, if they are different, you will directly receive different images in the saved location.

public boolean onTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub final int action = ev.getAction(); final int x = (int) ev.getX(); final int y = (int) ev.getY(); if (action == MotionEvent.ACTION_DOWN && x < this.getWidth()/4) { 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; } 

this will help you embed code from one place to another. And in the Drag method, specify x & y where you want to click it.

0
source

All Articles