未检测到DragEvent.ACTION_DROP

时间:2017-07-24 14:35:40

标签: android

由于某种原因,未检测到将视图A丢弃到视图B上。

在视图A和视图B的侦听器中,拖动侦听器都可以检测到拖动的开始和结束。

当我使用API​​ v24(Nougat 7.0)在设备/模拟器上测试应用程序时,它只检测ACTION_DROP,但是在API v23上测试时没有检测到丢弃事件。

启动拖动和启动的代码放弃查看A

private View.OnLongClickListener onRoutineLongClick() {
    return new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View view) {

            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                    view);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                view.startDragAndDrop(data, shadowBuilder, view, 0);
            } else {
                view.startDrag(data, shadowBuilder, view, 0);
            }

            return true;
        }
    };
}

onDragListener for View A

view.setOnDragListener(new View.OnDragListener() {
    @Override
    public boolean onDrag(View view, DragEvent dragEvent) {

        final View v = (View) dragEvent.getLocalState();;

        Log.d("", "Action: " + dragEvent.toString());

        switch (dragEvent.getAction()) {

            case DragEvent.ACTION_DRAG_STARTED:
                v.setVisibility(View.INVISIBLE);
                mBinImage.setVisibility(View.VISIBLE);
                return true;

            case DragEvent.ACTION_DRAG_ENDED:

                // Move back to position
                v.post(new Runnable() {
                    @Override
                    public void run() {
                        v.setVisibility(View.VISIBLE);
                        mBinImage.setVisibility(View.GONE);
                    }
                });

                return false;
        }

        return false;
    }
});

视图B的onDragListener

mBinImage.setOnDragListener(new View.OnDragListener() {
    @Override
    public boolean onDrag(View view, DragEvent dragEvent) {

        if (dragEvent.getAction() == DragEvent.ACTION_DROP) {

            // Do stuff
        }

        return true;
    }
});

0 个答案:

没有答案