OnDragListener发火几次?

时间:2015-08-31 09:13:21

标签: java android

我有几种布局;每个都包含TextView。如果用户将TextView拖到另一个TextView,我将执行方法swapFamily()会多次触发,具体取决于家庭数量

FamilyItemLayout只是一个自定义LinearLayout,附加了String属性,并为该属性添加了“getter”和“setter”。

然而,我的问题是,

private class onDropFamily implements View.OnDragListener {

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

        TextView txtDragged = (TextView) event.getLocalState();
        TextView txtTarget = (TextView) view;
        String familyDragged = ((FamilyItemLayout) txtDragged.getParent()).getFamily();
        String familyTarget = ((FamilyItemLayout) txtTarget.getParent()).getFamily();

        switch (event.getAction()) {
            // Signals the start of a drag and drop operation.
            case DragEvent.ACTION_DRAG_STARTED:
                break;
            // Signals to a View that the drag point has entered the bounding box of the View.
            case DragEvent.ACTION_DRAG_ENTERED:
                view.setBackgroundResource(R.mipmap.pill_sun);
                break;
            // Signals that the user has moved the drag shadow outside the bounding box of the View.
            case DragEvent.ACTION_DRAG_EXITED:
                view.setBackgroundResource(R.mipmap.pill_sky);
                break;
            // Signals to a View that the user has released the drag shadow, and the drag point is within the bounding box of the View.
            case DragEvent.ACTION_DROP:
                break;
            // Signals to a View that the drag and drop operation has concluded.
            case DragEvent.ACTION_DRAG_ENDED:
                // Check if drag event was successful
                if (dropEventHandled(event)) {
                    fixedPlan.swapFamily(familyDragged, familyTarget);
                }
                txtDragged.setVisibility(View.VISIBLE);
                txtTarget.setBackgroundResource(R.mipmap.pill_sky);
                break;
        }
        return true;
    }

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

1 个答案:

答案 0 :(得分:0)

我认为您应该将这些代码放在ACTION_DROP而不是ACTION_DRAG_ENDED

http://developer.android.com/reference/android/view/DragEvent.html

  

收到ACTION_DRAG_STARTED事件的所有视图都会收到   ACTION_DRAG_ENDED事件,即使它们当前不可见   阻力结束。

还要记住为ACTION_DROP

返回正确的值
  

View应该从其onDragEvent(DragEvent)处理程序返回true或   OnDragListener.onDrag()侦听器,如果它接受了drop,则为false   它忽略了掉落。