0

I'm develop an ecommerce app and when I drag an item from recyclerview to a layout, I need to change the border color of the item dragged and change the background of the original space of this item.

This is my code when I start the drag:

@Override
    public boolean onLongClick(View v) {
        ClipData data = ClipData.newPlainText("", "");
        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);    

        //Start dragging the item touched
        v.startDrag(data,           //Data to be dragged
                shadowBuilder,  //Drag shadow
                v,              //Local data about the drag and drop operation
                0);             //No flags

        v.setVisibility(View.INVISIBLE);

        return true;
    }

I have tried to create my own ShadowBuilder, but when I start the drag the item, it has transparent background and not shown the item dragged:

    public class MyDragShadowBuilder extends View.DragShadowBuilder {

    private static Drawable shadow; // The drag shadow image, defined as a drawable thing
    private Context context;
    private View view;

    public MyDragShadowBuilder (Context ctx, View v) {
        super(v);
        this.context = ctx;
        this.view    = v;
        shadow = new ColorDrawable(R.drawable.border_cardview_red);
    }

    // Defines a callback that sends the drag shadow dimensions and touch point back to the system.
    @ Override
    public void onProvideShadowMetrics (Point size, Point touch){
        int width, height;

        width  = getView().getWidth() / 2;  //Width of the shadow
        height = getView().getHeight() / 2; //Height of the shadow

        // The drag shadow is a ColorDrawable. This sets its dimensions to be the same as the
        // Canvas that the system will provide. As a result, the drag shadow will fill the canvas.
        shadow.setBounds(0, 0, width, height);
        size.set(width, height);        //Sets the size parameter's width and height values. These get back to the syste through the size parameter.
        touch.set(width/2, height/2);   //Sets the touch point's position to be in the middle of the drag shadow
    }

    // Defines a callback that draws the drag shadow in a Canvas that the system constructs from the dimensions passed in onProvideShadowMetrics ().
    @ Override
    public void onDrawShadow (Canvas canvas) {
        shadow.draw(canvas);
    }
}

This is my bordercard_view_red:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/transparent" />
    <stroke android:width="1dp" android:color="@color/primary_color" />
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>

I don't know how drag the item with another border color and change the background of the blank space of this item dragged.

In this image you can see that I need :)

enter image description here

I know that you can help me :) Thanks in advance

1 Answers1

0

You can use custom View.DragShadowBuilder by extending it.

Following links will be helpful. http://developer.android.com/reference/android/view/View.DragShadowBuilder.html#onDrawShadow(android.graphics.Canvas)

Changing dragshadow in android, while dragging happens

(This dealt with different issue but the article in the stackoverflow would give you a hint about how to extend it. )

Basically, onDrawShadow in View.DragShadowBuilder is involved in the draw of the shadow image. You can make the shadow image with the border you want.

To get the size of the view, you can call getView or get the info from onProvideShadowMetrics().

Community
  • 1
  • 1