2014-03-31 86 views
0

我已經按照http://developer.android.com/guide/topics/ui/drag-drop.html的拖放教程進行了操作,現在我需要檢測用戶是否將視圖放在另一個視圖上以合併來自兩個視圖的數據。將視圖拖放到另一個視圖上

我該如何檢測?它不能是簡單的碰撞檢測,因爲視圖可能與多個不同的視圖發生衝突,所以用戶必須將其放置在另一個視圖的中心。

UPDATE

這是我正在使用的代碼。當我拖放一個按鈕時,我不能讓它放在容器的空白區域(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; 
     } 
    } 
} 

回答

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> 
+0

我有以下代碼:https://gist.github.com/julianonunes/f29e279851ec313edcb3。如果我將按鈕拖到GridLayout的空白區域,它也被視爲有效的放置位置。我怎樣才能解決這個問題? –

相關問題