拖动视图并将其放在另一个视图上

时间:2014-03-31 12:33:50

标签: android

我已经按照http://developer.android.com/guide/topics/ui/drag-drop.html的拖放教程,现在我需要检测用户是否将视图放在另一个视图上以合并两个视图中的数据。

我怎样才能发现这个?它不能是简单的碰撞检测,因为视图可能会与多个不同的视图发生碰撞,因此用户必须将其置于另一个视图的中心位置。

更新

这是我正在使用的代码。当我拖放一个按钮时,我不能让它被放在容器的空白区域(GridLayout)。我该如何解决这个问题?

public class MainActivity extends Activity {

    private Button mButton1;
    private Button mButton2;
    private Button mButton3;
    private Button mButton4;
    private Button mButton5;
    private Button mButton6;
    private Button mButton7;
    private Button mButton8;
    private Button mButton9;
    private Button mButton10;
    private Button mButton11;
    private Button mButton12;
    private Button mButton13;
    private Button mButton14;
    private Button mButton15;
    private Button mButton16;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        loadReferences();
        setEventHandlers();
    }

    private void setEventHandlers() {
        mButton1.setOnTouchListener(new MyTouchListener());
        mButton2.setOnTouchListener(new MyTouchListener());
        mButton3.setOnTouchListener(new MyTouchListener());
        mButton4.setOnTouchListener(new MyTouchListener());
        mButton5.setOnTouchListener(new MyTouchListener());
        mButton6.setOnTouchListener(new MyTouchListener());
        mButton7.setOnTouchListener(new MyTouchListener());
        mButton8.setOnTouchListener(new MyTouchListener());
        mButton9.setOnTouchListener(new MyTouchListener());
        mButton10.setOnTouchListener(new MyTouchListener());
        mButton11.setOnTouchListener(new MyTouchListener());
        mButton12.setOnTouchListener(new MyTouchListener());
        mButton13.setOnTouchListener(new MyTouchListener());
        mButton14.setOnTouchListener(new MyTouchListener());
        mButton15.setOnTouchListener(new MyTouchListener());
        mButton16.setOnTouchListener(new MyTouchListener());

        mButton1.setOnDragListener(new MyDragListener());
        mButton2.setOnDragListener(new MyDragListener());
        mButton3.setOnDragListener(new MyDragListener());
        mButton4.setOnDragListener(new MyDragListener());
        mButton5.setOnDragListener(new MyDragListener());
        mButton6.setOnDragListener(new MyDragListener());
        mButton7.setOnDragListener(new MyDragListener());
        mButton8.setOnDragListener(new MyDragListener());
        mButton9.setOnDragListener(new MyDragListener());
        mButton10.setOnDragListener(new MyDragListener());
        mButton11.setOnDragListener(new MyDragListener());
        mButton12.setOnDragListener(new MyDragListener());
        mButton13.setOnDragListener(new MyDragListener());
        mButton14.setOnDragListener(new MyDragListener());
        mButton15.setOnDragListener(new MyDragListener());
        mButton16.setOnDragListener(new MyDragListener());
    }

    private void loadReferences() {
        mButton1 = (Button) findViewById(R.id.button);
        mButton2 = (Button) findViewById(R.id.button2);
        mButton3 = (Button) findViewById(R.id.button3);
        mButton4 = (Button) findViewById(R.id.button4);
        mButton5 = (Button) findViewById(R.id.button5);
        mButton6 = (Button) findViewById(R.id.button6);
        mButton7 = (Button) findViewById(R.id.button7);
        mButton8 = (Button) findViewById(R.id.button8);
        mButton9 = (Button) findViewById(R.id.button9);
        mButton10 = (Button) findViewById(R.id.button10);
        mButton11 = (Button) findViewById(R.id.button11);
        mButton12 = (Button) findViewById(R.id.button12);
        mButton13 = (Button) findViewById(R.id.button13);
        mButton14 = (Button) findViewById(R.id.button14);
        mButton15 = (Button) findViewById(R.id.button15);
        mButton16 = (Button) findViewById(R.id.button16);
    }

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

    class MyDragListener implements View.OnDragListener {

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

                    break;
                case DragEvent.ACTION_DRAG_EXITED:

                    break;
                case DragEvent.ACTION_DROP:
                    // Dropped, reassign View to ViewGroup
                    View view = (View) event.getLocalState();
                    ViewGroup owner = (ViewGroup) view.getParent();
                    owner.removeView(view);
                    Toast.makeText(MainActivity.this, ((Button)view).getText(), 1000).show();

                    view.setVisibility(View.VISIBLE);
                    break;
                case DragEvent.ACTION_DRAG_ENDED:

                default:
                    break;
            }
            return true;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

试试这个。

public class ShapeDragDrop extends Activity implements OnTouchListener,
        OnDragListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shape_drag_drop);
        findViewById(R.id.red_ball).setOnTouchListener(this);
        findViewById(R.id.green_ball).setOnTouchListener(this);
        findViewById(R.id.blue_ball).setOnTouchListener(this);
        findViewById(R.id.top_container).setOnDragListener(this);
        findViewById(R.id.bottom_container).setOnDragListener(this);
    }

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

    @Override
    public boolean onDrag(View v, DragEvent e) {
        if (e.getAction() == DragEvent.ACTION_DROP) {
            View view = (View) e.getLocalState();
            ViewGroup from = (ViewGroup) view.getParent();
            from.removeView(view);
            LinearLayout to = (LinearLayout) v;
            to.addView(view);
            view.setVisibility(View.VISIBLE);
        }
        return true;
    }

}


buleball.xml like shape in drawable folder.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <stroke
        android:width="4dp"
        android:color="#0000FF" />

    <gradient
        android:angle="270"
        android:endColor="#800000FF"
        android:startColor="#FF0000FF" />

</shape>


layout.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="10dp"
        android:text="Drag-N-Drop Demo"
        android:textSize="20sp" />

    <LinearLayout
        android:id="@+id/top_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        android:layout_weight=".5"
        android:background="#000000"
        android:gravity="center"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/red_ball"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:src="@drawable/red_ball" />

        <ImageView
            android:id="@+id/green_ball"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:src="@drawable/green_ball" />

        <ImageView
            android:id="@+id/blue_ball"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:src="@drawable/blue_ball" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        android:layout_weight=".5"
        android:background="#000000"
        android:gravity="center"
        android:orientation="horizontal" >
    </LinearLayout>

</LinearLayout>