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