Vinay Kelkar Vinay Kelkar - 2 months ago 9
Android Question

How to move the entire view using drag shadow builder in android?

Now, I am using a drag shadow builder class in Android and I can swap the data between views. But, I want to swap the entire view rather than just the data. Can anyone help me?

For example, if I have 4 TextViews, A,B,C,D and I want move from A to C, then, using the Drag Shadow Builder I can move only data inside A to C. But, I want to move the complete textview from one position to another. How do I achieve it?

//Here we declare On touch Listener to listen to the touch events

View.OnTouchListener longListen = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK ) {
case MotionEvent.ACTION_DOWN: {

DragShadow ds = new DragShadow(v);
ClipData data = ClipData.newPlainText("","");
v.startDrag(data,ds,v,0);
return false;
}

case MotionEvent.ACTION_MOVE:
break;

case MotionEvent.ACTION_CANCEL:
break;

case MotionEvent.ACTION_UP:
break;
}

return true;
}
};


// Here we set the drag listener to listen to the drop of the event

View.OnDragListener dropListener = new View.OnDragListener() {


@Override
public boolean onDrag(View v, DragEvent event) {

int dragEvent = event.getAction();

switch (dragEvent)
{
//case DragEvent

case DragEvent.ACTION_DRAG_STARTED:
break;

case DragEvent.ACTION_DRAG_ENTERED :
break;

case DragEvent.ACTION_DRAG_ENDED:
break;

case DragEvent.ACTION_DRAG_EXITED:
break;

case DragEvent.ACTION_DRAG_LOCATION:
break;

case DragEvent.ACTION_DROP:

View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();


Button target = (Button) v;
String target_og = target.getText().toString();
Button dragged = (Button) event.getLocalState();
String dragged_og = dragged.getText().toString();
dragged.setText(target_og);
target.setText(dragged_og);
return true;

}

return true;
}
};

Answer

Ok, so what you need to do is something like this:

 private class DragShadow extends View.DragShadowBuilder {
    View greyBox;

    public DragShadow(View view) {
        super(view);
        greyBox = view;
    }

    @Override
    public void onDrawShadow(Canvas canvas) {
        greyBox.draw(canvas);
    }

    @Override
    public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
        View v = getView();
        int height = v.getHeight();
        int width = v.getWidth();
        shadowSize.set(width, height);
        shadowTouchPoint.set((width / 2), (height / 2));
    }
}


View.OnLongClickListener longListen = new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        DragShadow ds = new DragShadow(v);
        ClipData data = ClipData.newPlainText("","");
        v.startDrag(data,ds,v,0);
        v.setVisibility(View.INVISIBLE);
        return false;
    }

};


 View.OnDragListener dropListener1 = new View.OnDragListener() {
    @Override
    public boolean onDrag(View v, DragEvent event) {
        int dragEvent = event.getAction();
        switch (dragEvent)
        {
            case DragEvent.ACTION_DRAG_STARTED:
            case DragEvent.ACTION_DRAG_ENTERED :
            case DragEvent.ACTION_DRAG_LOCATION:
            case DragEvent.ACTION_DRAG_ENDED:
            case DragEvent.ACTION_DRAG_EXITED:
                break;
            case DragEvent.ACTION_DROP:
                View dragged = (View) event.getLocalState();
                LinearLayout owner = (LinearLayout) dragged.getParent();
                int draggedPosition = owner.indexOfChild(dragged);
                int targetPosition = owner.indexOfChild(v);
                owner.removeView(dragged);
                owner.removeView(v);
                if (draggedPosition > targetPosition) {
                    owner.addView(dragged, targetPosition);
                    owner.addView(v, draggedPosition);
                } else {
                    owner.addView(dragged, targetPosition - 1);
                    owner.addView(v, draggedPosition);
                }
                dragged.setVisibility(View.VISIBLE);
        }
        return true;
    }
};
Comments