无法实现拖放

时间:2018-05-09 10:56:49

标签: android

我写了一个简单的拖放代码。虽然我可以拖动我的图像视图,但我不能将它放在一个新的地方(只有阴影移动)。有人可以帮助我吗?

以下是实现拖放的代码的主要部分:

    img.setOnDragListener(new View.OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch(event.getAction())
            {
                case DragEvent.ACTION_DRAG_STARTED:
                    layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                    Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED");
                    break;
                case DragEvent.ACTION_DRAG_ENTERED:
                    Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED");
                    int x_cord = (int) event.getX();
                    int y_cord = (int) event.getY();
                    break;
                case DragEvent.ACTION_DRAG_EXITED :
                    Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED");
                    x_cord = (int) event.getX();
                    y_cord = (int) event.getY();
                    layoutParams.leftMargin = x_cord;
                    layoutParams.topMargin = y_cord;
                    v.setLayoutParams(layoutParams);
                    break;
                case DragEvent.ACTION_DRAG_LOCATION :
                    Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION");
                    x_cord = (int) event.getX();
                    y_cord = (int) event.getY();
                    break;
                case DragEvent.ACTION_DRAG_ENDED :
                    Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENDED");
                    break;
                case DragEvent.ACTION_DROP:
                    Log.d(msg, "ACTION_DROP event");
                    break;
                default:
                    break;
            }
            return true;
        }
    });


}
}

在“日志”窗口中,仅报告“操作是DragEvent.ACTION_DRAG_ENDED”。我不明白为什么会这样!

以下是日志窗口中显示的输出结果:

05-10 05:33:45.339 2585-2585/com.example.lenovo.dragdrop_1 I/ViewRootImpl[MainActivity]: Reporting drop result: false

                                                                                     [ 05-10 05:33:45.864  2585: 2585 D/         ]
                                                                                     Action is DragEvent.ACTION_DRAG_ENDED
05-10 05:33:45.864 2585-2585/com.example.lenovo.dragdrop_1 D/gralloc_ranchu: `gralloc_unregister_buffer: exiting HostConnection (is buffer-handling thread)`

可以由开始拖放的方法引起吗?!实现拖放的代码如下所示。虽然'startDragAndDrop'需要API级别24(我的最小SDK为16),但是方法'startDrag'会显示一条交叉线,表示该方法已被弃用。

img.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
            String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
            ClipData dragData = new ClipData(v.getTag().toString(), mimeTypes, item);

            // Instantiates the drag shadow builder.
            View.DragShadowBuilder myShadow = new View.DragShadowBuilder(img);

            // Starts the drag
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
                v.startDragAndDrop(dragData,myShadow,null,0);
            else
                v.startDrag(dragData,myShadow,null,0);

            return true;
        }
    });

0 个答案:

没有答案