嗨,正如您在上面的屏幕截图中看到的那样,我有两个linearlayout对象,左侧边栏是一个,而较大的区域,中间的平板电脑图标是另一个。
我想要的是能够将左侧栏上任意2个ImageVies的副本拖到linearlayout对象上。
当我尝试丢弃图像消失时出现问题,我查看了z-index问题,但我不认为这是什么。我很感激任何帮助。
MainActivity.java
注意:我知道我可以创建一个myTouchListener类,而不是每次设置触摸监听器时都实现接口,但为了简单起见没有,因为我是初学的android开发人员。
public class MainActivity extends AppCompatActivity {
ImageView arduinoLogo;
ImageView piLogo;
LinearLayout mainDrop;
String msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arduinoLogo = (ImageView) findViewById(R.id.arduinoLogo);
piLogo = (ImageView) findViewById(R.id.piLogo);
mainDrop = (LinearLayout) findViewById(R.id.mainDrop);
mainDrop.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
switch(event.getAction()){
case DragEvent.ACTION_DRAG_STARTED:
Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED");
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED");
break;
case DragEvent.ACTION_DRAG_LOCATION :
Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION");
break;
case DragEvent.ACTION_DROP: {
View view = (View) event.getLocalState();
ViewGroup viewgroup = (ViewGroup) view.getParent();
viewgroup.removeView(view);
LinearLayout containView = (LinearLayout) v;
containView.addView(view);
view.setVisibility(View.VISIBLE);
break;
}
case DragEvent.ACTION_DRAG_ENDED: {
Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENDED");
break;
}
default:
break;
}
return true;
}
});
arduinoLogo.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
ClipData clipdata = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(arduinoLogo);
if (android.os.Build.VERSION.SDK_INT > 23) {
v.startDragAndDrop(clipdata, shadowBuilder, arduinoLogo, 0);
}
v.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
});
piLogo.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
ClipData clipdata = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(arduinoLogo);
if (android.os.Build.VERSION.SDK_INT > 23) {
v.startDragAndDrop(clipdata, shadowBuilder, arduinoLogo, 0);
}
v.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
});
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.proviz.MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:context=".MainActivity"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:id="@+id/mainSidebar"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:background="#eee"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/arduinoLogo"
android:src="@drawable/arduinologo"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/piLogo"
android:layout_below="@id/arduinoLogo"
android:src="@drawable/pilogo"
android:background="#FFF"
android:layout_marginTop="1dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/mainDrop"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<!--android:background="#00aaaa"-->>
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center"
android:src="@drawable/thistablet"/>
</LinearLayout>
</LinearLayout>