在Android中消失按钮onTouch和onDrag

时间:2014-02-05 01:09:54

标签: android touch drag

我正在做一点拖动将图像放入布局中。 一切都运行正常,除非有时触摸它时图像会消失。

知道为什么吗?我似乎不明白为什么有时会消失,我想我会满足各种可能性。对你的帮助表示感谢。谢谢!

到目前为止,这是我的代码......

public class MainActivity extends Activity {

private static final String LOGCAT = null;

/*
 * Here are the links I've been using
 * http://www.techrepublic.com/blog/software-engineer/try-androids-useful-drag-and-drop-api/
 * http://codingjunkie.net/android-drag-and-drop-part-2/
 * http://javapapers.com/android/android-drag-and-drop/
 */

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.blue_ball).setOnTouchListener(myOnTouchListener);    // My image
    findViewById(R.id.top_container).setOnDragListener(myDragListener);    // My 1st linear layout 
    findViewById(R.id.bottom_container).setOnDragListener(myDragListener); // My 2nd linear layout with "grayed a" image 
}

OnTouchListener myOnTouchListener = new OnTouchListener() {

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                    view);
            view.startDrag(null, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        } else {
            return false;
        }
    }
};

OnDragListener myDragListener = new OnDragListener() {

    @Override
    public boolean onDrag(View layoutview, DragEvent dragevent) {

        int action = dragevent.getAction();
        View view = (View) dragevent.getLocalState();

        switch (action) {
        case DragEvent.ACTION_DRAG_STARTED:
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            break;
        case DragEvent.ACTION_DROP:
            ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            LinearLayout container = (LinearLayout) layoutview;
            container.addView(view);
            view.setVisibility(View.VISIBLE);
            if (container.getId() == R.id.bottom_container) {
                view.setOnTouchListener(null);
                view.setOnDragListener(null);
            }
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            Log.d(LOGCAT, "Drag ended");
            if (dropEventNotHandled(dragevent)) {
                view.setVisibility(View.VISIBLE);
            }
            break;
        default:
            break;
        }
        return true;
    }

    private boolean dropEventNotHandled(DragEvent dragEvent) {
        return !dragEvent.getResult();
    }
};

}

这是我的错误讯息:

02-05 14:48:03.523: E/WindowManager(410): Unable to transfer touch focus
02-05 14:48:03.523: W/WindowManager(410): Drag is in progress but there is no drag window handle.

1 个答案:

答案 0 :(得分:1)

感谢JRowan,我尝试过但没有工作,但帮助我意识到问题出在哪里。 在尝试了很多不同的东西之后,我想我需要将它添加到OnTouchListener:

if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
        view.setVisibility(View.VISIBLE);

非常简单,没有更多消失的按钮。 感谢