【问题标题】:Android Drag a View and change his ParentAndroid 拖动视图并更改其父级
【发布时间】:2013-03-28 13:19:35
【问题描述】:

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

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

所以我需要将 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 并继续拖动,我将不胜感激。

谢谢!

【问题讨论】:

  • 您可以编辑您的问题并包含您现在使用的代码吗?
  • @FoamyGuy 好的,我放了一段代码,如果您需要更多信息,我会再次编辑。

标签: android android-layout


【解决方案1】:

使用 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;
                }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-19
    • 2023-04-07
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多