拖放导致按钮消失

时间:2015-01-30 01:37:15

标签: android drag-and-drop visibility

我尝试使用一些按钮实现拖放操作。我的每个按钮的OnTouchListener如下所示

private final class MyTouchListener implements View.OnTouchListener {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData clipData = ClipData.newPlainText("", "");
            View.DragShadowBuilder dsb = new View.DragShadowBuilder(view);
            view.startDrag(clipData, dsb, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        } else {
            return false;
        }
    }
}

到目前为止工作正常,但我的OnDragListener有问题。当拖动按钮切换布局时,我没有问题,但是当点击按钮时,它有时(不总是)变得不可见。我怀疑它是ACTION_DRAG_ENDED但不知道。这是我的OnDragListener

代码
private final class MyDragListener implements View.OnDragListener {
    private boolean containsDragable = true;

    @Override
    public boolean onDrag(View view, DragEvent dragEvent) {

        int dragAction = dragEvent.getAction();
        View dragView = (View) dragEvent.getLocalState();
        if (dragAction == DragEvent.ACTION_DRAG_EXITED) {
            containsDragable = false;
        } else if (dragAction == DragEvent.ACTION_DRAG_ENTERED) {
            containsDragable = true;
        } else if (dragAction == DragEvent.ACTION_DRAG_ENDED) {
            if (!dragEvent.getResult()) {
                dragView.setVisibility(View.VISIBLE);
            }
            dragView.setVisibility(View.VISIBLE);
        } else if (dragAction == DragEvent.ACTION_DROP && containsDragable) {
            rearrangeButtons((LinearLayout) view, dragView);
            dragView.setVisibility(View.VISIBLE);
        }
        return true;
    }

private void rearrangeButtons(LinearLayout view, View dragView) {
        ViewGroup owner = (ViewGroup) dragView.getParent();
        owner.removeView(dragView);
        view.addView(dragView);

        View v = view.getChildAt(0);
        Button button = (Button) v;
        view.removeView(v);
        owner.addView(v);
        v.setVisibility(View.VISIBLE);
    }

每个按钮都包含在自己的LinearLayout中,不确定是否是原因。

感谢任何可以提供帮助的人!

0 个答案:

没有答案