我正在尝试拖动下面 XML 中给出的 linear_layout_mouse。我想使用布局中给出的中间按钮拖动整个布局。我可以拖动布局,但是当我放下它时,布局消失了。在 logcat 中,当被丢弃时,我得到的日志为“报告丢弃结果:假”。我为此使用了 DragShadowBuilder。
Can anyone help me out to fix this problem.?
Code:
// On touch listener
oMiddleButton.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(mouseLayout);
mouseLayout.startDrag(null, shadowBuilder, mouseLayout, 0);
mouseLayout.setVisibility(View.INVISIBLE);
return true;
}
else
{
return false;
}
}
});
oMiddleButton.setOnDragListener(new OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
System.out.println("Drag event started");
break;
case DragEvent.ACTION_DRAG_ENTERED:
System.out.println("Drag event entered into "+mouseLayout.toString());
break;
case DragEvent.ACTION_DRAG_EXITED:
System.out.println("Drag event exited from "+mouseLayout.toString());
break;
case DragEvent.ACTION_DROP:
System.out.println("Dropped");
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
RelativeLayout container = (RelativeLayout) findViewById(R.id.relative_layout_shared_screen);
container.removeView(view);
container.addView(mouseLayout);
mouseLayout.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
System.out.println("Drag ended");
break;
case DragEvent.ACTION_DRAG_LOCATION:
mouseLayout.setVisibility(View.VISIBLE);
break;
default:
break;
}
return true;
}
});
// XML 布局文件
<RelativeLayout
android:id="@+id/relative_layout_shared_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<ImageView
android:id="@+id/image_shared_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/temp" />
<LinearLayout
android:id="@+id/linear_layout_mouse"
android:layout_width="99dp"
android:layout_height="60dp"
android:orientation="horizontal"
android:layout_centerInParent="true"
android:visibility="gone"
android:background="@drawable/mouse_controller" >
<LinearLayout
android:id="@+id/left_panel_container"
android:layout_width="33dp"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/left_top_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="33dp"
android:gravity="center"
android:background="#07000000"/>
<Button
android:id="@+id/left_bottom_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="27dp"
android:gravity="center"
android:background="#07000000"/>
</LinearLayout>
<LinearLayout
android:id="@+id/middle_panel_container"
android:layout_width="33dp"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/middle_top_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="27dp"
android:background="#07000000"/>
<Button
android:id="@+id/middle_middle_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="9dp"
android:background="#07000000"/>
<Button
android:id="@+id/middle_bottom_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="25dp"
android:background="#07000000"/>
</LinearLayout>
<LinearLayout
android:id="@+id/right_panel_container"
android:layout_width="33dp"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/right_top_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="33dp"
android:background="#07000000"/>
<Button
android:id="@+id/right_bottom_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="27dp"
android:background="#07000000"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>