class MyDragListener implements View.OnDragListener {
private float dX;
private float dY;
ImageView tmpView;
@Override
public boolean onDrag(View view, DragEvent event) {
// Handles each of the expected events
switch (event.getAction()) {
//signal for the start of a drag and drop operation.
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
//the drag point has entered the bounding box of the View
case DragEvent.ACTION_DRAG_ENTERED:
//v.setBackground(targetShape); //change the shape of the view
break;
//the user has moved the drag shadow outside the bounding box of the View
case DragEvent.ACTION_DRAG_EXITED:
//v.setBackground(normalShape); //change the shape of the view back to normal
break;
//drag shadow has been released,the drag point is within the bounding box of the View
case DragEvent.ACTION_DROP:
dX = view.getX() - event.getX();
dY = view.getY() - event.getY();
tmpView = new ImageView(getActivity());
tmpView.setImageDrawable(((ImageView) view).getDrawable());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(150, 150);
params.leftMargin = (int) event.getX();
params.topMargin = (int) event.getY();
mFrameLayout.addView(tmpView, params);
RelativeLayout.LayoutParams ivLayoutParams = (RelativeLayout.LayoutParams) tmpView.getLayoutParams();
ivLayoutParams.leftMargin = (int) (event.getX());
ivLayoutParams.topMargin = (int) (event.getY() - 250);
tmpView.setLayoutParams(ivLayoutParams);
break;
//the drag and drop operation has concluded.
case DragEvent.ACTION_DRAG_ENDED:
//v.setBackground(normalShape); //go back to normal shape
default:
break;
}
return true;
}
}
此处mFrameLayout
是我的xml中的frameLayout。
<FrameLayout
android:layout_width="350dp"
android:layout_height="250dp"
android:layout_marginTop="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/frameLayout"
android:clickable="true"
android:longClickable="true">
</FrameLayout>
Drag可以正常工作,但是当放入frameLayout时,它会抛出.FrameLayout cannot be cast to ImageView
到这一行:
tmpView.setImageDrawable(((ImageView) view).getDrawable());
Draglistener在框架布局中注册为mFrameLayout.setOnDragListener(new MyDragListener());
我已经读过,删除图像FrameLayout是最好的,但我无法将图像从其他布局中删除到FrameLayout。
更新:
现在我的错误没有出现,但图片也没有出现在放置位置:
class MyDragListener implements View.OnDragListener {
private float dX;
private float dY;
ImageView tmpView;
@Override
public boolean onDrag(View view, DragEvent event) {
// Handles each of the expected events
switch (event.getAction()) {
//signal for the start of a drag and drop operation.
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
//the drag point has entered the bounding box of the View
case DragEvent.ACTION_DRAG_ENTERED:
//v.setBackground(targetShape); //change the shape of the view
break;
//the user has moved the drag shadow outside the bounding box of the View
case DragEvent.ACTION_DRAG_EXITED:
//v.setBackground(normalShape); //change the shape of the view back to normal
break;
//drag shadow has been released,the drag point is within the bounding box of the View
case DragEvent.ACTION_DROP:
View draggedView = (View) event.getLocalState();
if(mLinearLayout.getParent() != null){
ViewGroup parent = (ViewGroup) draggedView.getParent();
parent.removeView(draggedView);
mLinearLayout.removeView(draggedView);
}
mLinearLayout.addView(draggedView);
internalImageView.setVisibility(View.VISIBLE);
view.invalidate();
break;
//the drag and drop operation has concluded.
case DragEvent.ACTION_DRAG_ENDED:
//v.setBackground(normalShape); //go back to normal shape
default:
break;
}
return true;
}
}
我的xml。
<LinearLayout
android:orientation="horizontal"
android:layout_height="250dp"
android:id="@+id/linearLayoutPlaceHolder"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:layout_width="375dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="@drawable/cast_album_art_placeholder"
android:id="@+id/imageView2"
android:layout_weight="1" />
</LinearLayout>
StartDrag方法
mImageViewHolder.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
// Instantiates the drag shadow builder.
View.DragShadowBuilder myShadow = new DragShadowBuilder(mImageViewHolder);
// Starts the drag
view.startDrag(null, // the data to be dragged
myShadow, // the drag shadow builder
view, // need to use local data
0 // flags (not currently used, set to 0)
);
return true;
}
});
我现在可以从一个布局拖放到另一个布局,但只能拖放一个图像。我不能重复其他图像。
答案 0 :(得分:0)
找到解决方案。
Drag Listener现在写得像这样。
private LinearLayout mLinearLayout;
private ImageView internalImageView;
class MyDragListener implements View.OnDragListener {
@Override
public boolean onDrag(View view, DragEvent event) {
// Handles each of the expected events
switch (event.getAction()) {
//signal for the start of a drag and drop operation.
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
//the drag point has entered the bounding box of the View
case DragEvent.ACTION_DRAG_ENTERED:
break;
//the user has moved the drag shadow outside the bounding box of the View
case DragEvent.ACTION_DRAG_EXITED:
break;
//drag shadow has been released,the drag point is within the bounding box of the View
case DragEvent.ACTION_DROP:
View draggedView = (View) event.getLocalState();
if(mLinearLayout.getParent() != null){
ViewGroup parent = (ViewGroup) draggedView.getParent();
parent.removeView(draggedView);
mLinearLayout.removeAllViews();
//mLinearLayout.removeAllViewsInLayout();
}
mLinearLayout.addView(draggedView);
mLinearLayout.invalidate();
internalImageView.setVisibility(View.VISIBLE);
view.invalidate();
break;
//the drag and drop operation has concluded.
case DragEvent.ACTION_DRAG_ENDED:
//## Heading ##view.getRootView().findViewById(R.id.carouselView).invalidate();
break;
default:
break;
}
return true;
}
}