简体   繁体   中英

how to set drag and drop for ImageView in android studio?

I have an ImageView under recyclerView, in that imageView i have placed a imageView(icon) by constraining the layout.

if I drag the imageView to the imageView by using DragShadowBuilder now I want to toast a msg that imageView has selected.

I have tried this code but it not works and saying the Clipdata tag is null.

Please check the code below

    @Override
    public boolean onDrag(View v, DragEvent event)
    {
        Log.d(TAG, "onDrag");

        // Store the action type for the incoming event
        final int action = event.getAction();

        // Handles each of the expected events
        switch (action)
        {
            case DragEvent.ACTION_DRAG_STARTED:

                // In order to inform user that drag has started, we apply yellow tint to view
                ((ImageView) v).setColorFilter(Color.YELLOW);

                // Invalidate the view to force a redraw in the new tint
                v.invalidate();

                // Returns true to indicate that the View can accept the
                // dragged data.
                return true;

            case DragEvent.ACTION_DRAG_ENTERED:

                // Apply a gray tint to the View
                ((ImageView) v).setColorFilter(Color.LTGRAY);

                // Invalidate the view to force a redraw in the new tint
                v.invalidate();

                return true;

            case DragEvent.ACTION_DRAG_LOCATION:

                // Ignore the event
                return true;

            case DragEvent.ACTION_DRAG_EXITED:

                // Re-sets the color tint to yellow
                ((ImageView) v).setColorFilter(Color.YELLOW);

                // Invalidate the view to force a redraw in the new tint
                v.invalidate();

                return true;


            case DragEvent.ACTION_DROP:
                save_drag.setTag("bookmark");
// Create a new ClipData.
                // This is done in two steps to provide clarity. The convenience method
                // ClipData.newPlainText() can create a plain text ClipData in one step.

                // Create a new ClipData.Item from the ImageView object's tag
                ClipData.Item item = new ClipData.Item(v.getTag());

                // Create a new ClipData using the tag as a label, the plain text MIME type, and
                // the already-created item. This will create a new ClipDescription object within the
                // ClipData, and set its MIME type entry to "text/plain"
                ClipData dragData = new ClipData(v.getTag(),ClipData.MIMETYPE_TEXT_PLAIN,item);

                // Gets the item containing the dragged data
                ClipData dragData = event.getClipData();
                // Gets the text data from the item.
                final String tag = dragData.getItemAt(0).toString();

                // Displays a message containing the dragged data.
                Toast.makeText(mContext, "The dragged image is " + tag, Toast.LENGTH_SHORT).show();

                // Turns off any color tints
                ((ImageView) v).clearColorFilter();

                // Invalidates the view to force a redraw
                v.invalidate();

                return true;

            case DragEvent.ACTION_DRAG_ENDED:

                // Turns off any color tinting
                ((ImageView) v).clearColorFilter();

                // Invalidates the view to force a redraw
                v.invalidate();

                // Check for result
                if (event.getResult())
                {
                    Toast.makeText(mContext, "Bingo !", Toast.LENGTH_SHORT).show();

                }
                else
                {
                    Toast.makeText(mContext, "Oups, try again !", Toast.LENGTH_SHORT).show();
                    save_drag.setImageBitmap(null);

                }

                return true;

            default:
                break;
        }

        return false;
    }

you might want to try this, and put your toast method in the onClick method

ImageView image = (ImageView) mWidget.findViewById(R.id.imageBtn);
    image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM