视图在拖动列表中的位置发生更改



我正在父布局上放置视图。我想在长时间单击L1时拖动整个视图。当我长按L1时,视图进入拖动模式,但将触摸位置更改为视图的中心(x,y)。我想在按住布局(L1)的上栏的情况下拖动视图。

View:
    ______________
   |______L1______|
   |              |
y  |      L2      |
   |              |
   |______________|
           x

    @Override
    public boolean onDrag(View v, DragEvent event) {
        // Handles each of the expected events
        switch (event.getAction()) {
        //signal for the start of a drag and drop operation.
        case DragEvent.ACTION_DRAG_STARTED:
            // do nothing
            break;
        //the drag point has entered the bounding box of the View
        case DragEvent.ACTION_DRAG_ENTERED:
            v.setBackground(targetShape);   //change the shape of the view
            break;
        //the user has moved the drag shadow outside the bounding box of the View
        case DragEvent.ACTION_DRAG_EXITED:
            v.setBackground(normalShape);   //change the shape of the view back to normal
            break;
        //drag shadow has been released,the drag point is within the bounding box of the View
        case DragEvent.ACTION_DROP:
             doStuff();
              break;
        //the drag and drop operation has concluded.
        case DragEvent.ACTION_DRAG_ENDED:
            v.setBackground(normalShape);   //go back to normal shape
        default:
            break;
        }
        return true;
    }

您可以使用View.DragShadowBuilder:设置图像中的拖动点

view.onProvideShadowMetrics(/* size of shadow */,
/* provide Point object with x cordinate equal to whole
 width of L1 and y cordinate equal to height of L1*/);

通过这种方式,您可以将阴影触摸点设置为L1

您可以参考DragShadowBuilder 的开发人员页面

最新更新