2

我需要一种在子视图到父视图之间进行拖放的方法。所以我会放一张图片来更好地解释:

在此处输入图像描述

黄色代表一个扩展RelativeLayout 的类。黑色代表一个扩展 ViewGroup 的类 白色代表 ImageViews 红色代表一个 View

所以我需要将 White Blocks(ImageViews) 放在 Yellow View 上,但我只拖过 Black View,因为 ImageView 有一个像父亲一样的 BlackView,所以我现在的解决方案是更改父亲的拖动视图,但是当我添加视图时在 YellowView 上,这会取消我的拖动事件,所以当我在视图之间切换时需要继续拖动。请注意,WhiteView 的唯一方法是放置在 RedView 上,如果 Touch up 和 White view 远离 RedView 白色块将返回 BlackView。

所以,我使用从网上获得的这段代码:

    public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            touchDown(event);
            break;
        case MotionEvent.ACTION_MOVE:
            touchMove(event);
            break;
        case MotionEvent.ACTION_UP:
            touchUp(event);
            break;
    }
    if (aViewIsDragged())
        return true;
    return false;
}
private void touchDown(MotionEvent event) {
    initialX = (int)event.getRawX();
    initialY = (int)event.getRawY();
    lastTouchX = (int)event.getRawX() + (gridPageWidth);
    lastTouchY = (int)event.getRawY();
}

public boolean onLongClick(View v) {        
    if(positionForView(v) != -1) {
        movingView = true;
        dragged = positionForView(v);
        return true;
    }
    return false;
}
private void touchMove(MotionEvent event) {

    if (movingView && aViewIsDragged()) {
        lastTouchX = (int) event.getX();
        lastTouchY = (int) event.getY();

        moveDraggedView(lastTouchX, lastTouchY);
        manageSwapPosition(lastTouchX, lastTouchY);
        manageEdgeCoordinates(lastTouchX);
        manageDeleteZoneHover(lastTouchX, lastTouchY);
    }
}
private void moveDraggedView(int x, int y) {
    ImageView child = null;
    View childAt = getChildAt(dragged);
    int width = childAt.getMeasuredWidth();
    int height = childAt.getMeasuredHeight();
    int left = x - (1 * width / 2);
    int t = y - (1 * height / 2);
    childAt.layout(left, t, left + width, t + height);
    //TODO at this point if left less than 0, I detect part of view is out and I need to change this view to Yello
    if (left <0){
            child = (ImageView) childAt;
            ViewGroup parent = (ViewGroup) getParent();
            cloneimage = new ImageView(getContext());
            cloneimage.setBackgroundDrawable(child.getDrawable());
            parent.addView(cloneimage);             
    }
}

所以我认为这有点困难,但如果有人帮助我将 WhiteView 从 Black 更改为 Yellow 并继续拖动,我会很感激。

谢谢!

4

1 回答 1

0

使用 ViewDragHelper!

在这里你可以找到文档:https ://developer.android.com/reference/android/support/v4/widget/ViewDragHelper.html

像这样覆盖你的 onTouch 事件

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final int action = ev.getActionMasked();
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mDragHelper.cancel();
        return false;
    }
    return mDragHelper.shouldInterceptTouchEvent(ev);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    mDragHelper.processTouchEvent(ev);
    return true;
}

然后使用 viewDragViewHelper

ViewDragHelper.create(DragCoordinatorLayout.this, 1.0f, new ViewDragHelper.Callback() {

                @Override
                public boolean tryCaptureView(View child, int pointerId) {
                    return true;
                }

                @Override
                public int clampViewPositionHorizontal(View child, int left, int dx) {
                    return left;
                }

                @Override
                public int clampViewPositionVertical(View child, int top, int dy) {
                    return top;
                }
}
于 2017-02-23T14:23:28.790 回答