Android拖放可见/不可见

时间:2016-02-29 15:57:34

标签: android drag-and-drop

我使用拖放操作将视图拖动到目标上。 我很难弄清楚如何拖动原始视图时拖动它是不可见的,如果拖动结束则再次可见,但它不会被丢弃在目标上。

final TextView dragText1 = (TextView) v.findViewById(R.id.text_user_drags_1);
dragText1.setOnTouchListener(new MyTouchListener());
final TextView target1 = (TextView) v.findViewById(R.id.text_target_1);
target1.setOnDragListener(new MyDragListener());


 private final class MyTouchListener implements View.OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
           // view.setVisibility(View.INVISIBLE);
            return true;
        } else {
            //view.setVisibility(View.VISIBLE);
            return true;
        }
    }
}
class MyDragListener implements OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:

                TextView draggedView2 = (TextView) event.getLocalState();
                ViewGroup owner1 = (ViewGroup) draggedView2.getParent();
                owner1.setVisibility(View.INVISIBLE);
                Log.d(TAG, "DRAG STARTED");
                break;
            case DragEvent.ACTION_DRAG_ENTERED:

                Log.d(TAG, "DRAG ENTERED");
                break;
            case DragEvent.ACTION_DRAG_EXITED:

                Log.d(TAG, "DRAG EXITED");
                break;
            case DragEvent.ACTION_DROP:
                // Dropped, reassign View to ViewGroup
                TextView draggedView = (TextView) event.getLocalState();
                ViewGroup owner = (ViewGroup) draggedView.getParent();
                owner.setVisibility(View.INVISIBLE);
                Log.d(TAG, "DRAG DROPPED");
                break;

            case DragEvent.ACTION_DRAG_ENDED:
                if(event.getAction() == DragEvent.ACTION_DROP){
                    TextView draggedView3 = (TextView) event.getLocalState();
                    ViewGroup owner3 = (ViewGroup) draggedView3.getParent();
                    owner3.setVisibility(View.INVISIBLE);
                }
                TextView draggedView4 = (TextView) event.getLocalState();
                ViewGroup owner4 = (ViewGroup) draggedView4.getParent();
                owner4.setVisibility(View.VISIBLE);
                Log.d(TAG, "DRAG ENDED");
            default:
                break;
        }
        return true;
    }
}

1 个答案:

答案 0 :(得分:1)

您必须使用事件ACTION_DRAG_LOCATION。在此事件中,您将获得视图的拖动X,并将alpha应用于与拖动百分比成比例的视图。因此,如果您的视图被拖了50%,则应用0.5的alpha值;拖动75%,应用.25的alpha等等(alpha =(100f - drag_percent)/ 100f