Android 拖动一个视图并将其放到另一个视图上
我遵循了中的拖放教程,现在我需要检测用户是否将视图放到另一个视图上,以合并两个视图中的数据 我怎样才能检测到这个?它不能是一个简单的碰撞检测,因为视图可以与多个不同的视图碰撞,因此用户必须将其放置在另一个视图的中心 更新 这是我正在使用的代码。当我拖放一个按钮时,我不能允许它被拖放到容器的空白区域(GridLayout)。我怎样才能解决这个问题Android 拖动一个视图并将其放到另一个视图上,android,Android,我遵循了中的拖放教程,现在我需要检测用户是否将视图放到另一个视图上,以合并两个视图中的数据 我怎样才能检测到这个?它不能是一个简单的碰撞检测,因为视图可以与多个不同的视图碰撞,因此用户必须将其放置在另一个视图的中心 更新 这是我正在使用的代码。当我拖放一个按钮时,我不能允许它被拖放到容器的空白区域(GridLayout)。我怎样才能解决这个问题 公共类MainActivity扩展活动{ 专用按钮MBUTON1; 私人按钮2; 专用按钮mButton3; 专用按钮mButton4; 专用按钮mBu
公共类MainActivity扩展活动{
专用按钮MBUTON1;
私人按钮2;
专用按钮mButton3;
专用按钮mButton4;
专用按钮mButton5;
专用按钮mButton6;
专用按钮mButton7;
专用按钮mButton8;
专用按钮mButton9;
专用按钮mButton10;
专用按钮mButton11;
专用按钮mButton12;
私人按钮13;
专用按钮14;
专用按钮mButton15;
专用按钮mButton16;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadReferences();
setEventHandlers();
}
私有void setEventHandlers(){
mButton1.setOnTouchListener(新的MyTouchListener());
mButton2.setOnTouchListener(新的MyTouchListener());
mButton3.setOnTouchListener(新的MyTouchListener());
mButton4.setOnTouchListener(新的MyTouchListener());
mButton5.setOnTouchListener(新的MyTouchListener());
mButton6.setOnTouchListener(新的MyTouchListener());
mButton7.setOnTouchListener(新的MyTouchListener());
mButton8.setOnTouchListener(新的MyTouchListener());
mButton9.setOnTouchListener(新的MyTouchListener());
mButton10.setOnTouchListener(新的MyTouchListener());
mButton11.setOnTouchListener(新的MyTouchListener());
mButton12.setOnTouchListener(新的MyTouchListener());
mButton13.setOnTouchListener(新的MyTouchListener());
mButton14.setOnTouchListener(新的MyTouchListener());
mButton15.setOnTouchListener(新的MyTouchListener());
mButton16.setOnTouchListener(新的MyTouchListener());
mButton1.setOnDragListener(新的MyDragListener());
mButton2.setOnDragListener(新的MyDragListener());
mButton3.setOnDragListener(新的MyDragListener());
mButton4.setOnDragListener(新的MyDragListener());
mButton5.setOnDragListener(新的MyDragListener());
mButton6.setOnDragListener(新的MyDragListener());
mButton7.setOnDragListener(新的MyDragListener());
mButton8.setOnDragListener(新的MyDragListener());
mButton9.setOnDragListener(新的MyDragListener());
mButton10.setOnDragListener(新的MyDragListener());
mButton11.setOnDragListener(新的MyDragListener());
mButton12.setOnDragListener(新的MyDragListener());
mButton13.setOnDragListener(新的MyDragListener());
mButton14.setOnDragListener(新的MyDragListener());
mButton15.setOnDragListener(新的MyDragListener());
mButton16.setOnDragListener(新的MyDragListener());
}
私有void loadReferences(){
mButton1=(按钮)findviewbyd(R.id.Button);
mButton2=(按钮)findviewbyd(R.id.button2);
mButton3=(按钮)findViewById(R.id.button3);
mButton4=(按钮)findViewById(R.id.button4);
mButton5=(按钮)findViewById(R.id.button5);
mButton6=(按钮)findViewById(R.id.button6);
mButton7=(按钮)findViewById(R.id.button7);
mButton8=(按钮)findViewById(R.id.button8);
mButton9=(按钮)findViewById(R.id.button9);
mButton10=(按钮)findViewById(R.id.button10);
mButton11=(按钮)findViewById(R.id.button11);
mButton12=(按钮)findViewById(R.id.button12);
mButton13=(按钮)findViewById(R.id.button13);
mButton14=(按钮)findViewById(R.id.button14);
mButton15=(按钮)findViewById(R.id.button15);
mButton16=(按钮)findViewById(R.id.button16);
}
私有最终类MyTouchListener实现View.OnTouchListener{
公共布尔onTouch(视图、运动事件、运动事件){
if(motionEvent.getAction()==motionEvent.ACTION\u向下){
ClipData data=ClipData.newPlainText(“,”);
View.DragShadowBuilder shadowBuilder=新视图.DragShadowBuilder(视图);
view.startDrag(数据,shadowBuilder,视图,0);
view.setVisibility(view.INVISIBLE);
返回true;
}否则{
返回false;
}
}
}
类MyDragListener实现View.OnDragListener{
@凌驾
公共布尔onDrag(视图v,DrageEvent事件){
int action=event.getAction();
开关(event.getAction()){
案例DrageEvent.ACTION\u DRAG\u已启动:
//无所事事
打破
案例DrageEvent.ACTION\u DRAG\u输入:
打破
案例DrageEvent.ACTION\u DRAG\u退出:
打破
案例DrageEvent.ACTION_DROP:
//已删除,请将视图重新指定给视图组
视图=(视图)事件。getLocalState();
ViewGroup所有者=(ViewGroup)view.getParent();
所有者。移除视图(视图);
Toast.makeText(MainActivity.this,((按钮)视图).getText(),1000.show();
view.setVisibility(view.VISIBLE);
打破
案例DrageEvent.ACTION\u DRAG\u已结束:
违约:
打破
}
返回true;
}
}
}
试试这个
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>
public类ShapeDragDrop扩展活动实现