我在xamarin android应用程序中工作。当从一个linearlayout拖动一个图像并删除到另一个布局时先删除并删除后添加到其他布局。
上的错误 LinearLayout container = (LinearLayout)v;
public bool OnDrag(View v, DragEvent e)
{
Drawable enterShape= Resources.GetDrawable(Resource.Drawable.shape_droptarget);
Drawable normalShape = Resources.GetDrawable(Resource.Drawable.shape);
var action = e.Action;
switch (e.Action)
{
case DragAction.Started:
//do nothing;
break;
case DragAction.Entered:
v.SetBackgroundDrawable(enterShape);
break;
case DragAction.Exited:
v.SetBackgroundDrawable(normalShape);
break;
case DragAction.Drop:
//drop reassign view to viewgroup
View view = (View)e.LocalState;
ViewGroup owner = (ViewGroup)view.Parent;
owner.RemoveView(view);
LinearLayout container = (LinearLayout)v;
container.AddView(view);
view.Visibility = ViewStates.Visible;
break;
case DragAction.Ended:
v.SetBackgroundDrawable(normalShape);
break;
default:
break;
}
return true;
}
public bool OnTouch(View vi, MotionEvent e)
{
if (e.Action == MotionEventActions.Down)
{
ClipData data = ClipData.NewPlainText("", "");
Android.Views.View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(vi);
vi.StartDrag(data, shadowBuilder, vi, 0);
vi.Visibility = ViewStates.Invisible;
return true;
}
else
return false;
}
答案 0 :(得分:0)
我在数据变量和访问ID中传递id字段以获取图像,然后用下一个替换。
public bool OnTouch(View v,MotionEvent e) { //抛出新的NotImplementedException(); if(e.Action == MotionEventActions.Down) { v.DrawingCacheEnabled = true; var data = ClipData.NewPlainText(" ID",v.Id.ToString()); // MyShadowBuilder是用户定义的Shadow builder类 Android.Views.View.DragShadowBuilder shadow = new MyShadowBuilder(v); v.StartDrag(data,shadow,null,0); 返回true; } 其他 返回false; } public bool OnDrag(View v,DragEvent e) { //抛出新的NotImplementedException(); v.DrawingCacheEnabled = true; Drawable enterShape = Resources.GetDrawable(Resource.Drawable.shape_droptarget); Drawable normalShape = Resources.GetDrawable(Resource.Drawable.shape);
var action = e.Action;
switch (e.Action)
{
case DragAction.Started:
//do nothing;
break;
case DragAction.Entered:
v.SetBackgroundDrawable(enterShape);
break;
case DragAction.Exited:
v.SetBackgroundDrawable(normalShape);
break;
case DragAction.Drop:
var data = e.ClipData.GetItemAt(0).Text;// Event.ClipData.GetItemAt(0).Text;
ImageView vtemp = this.FindViewById<ImageView>((int.Parse(data)));
View V1 = (View)(v);
ViewGroup owner = (ViewGroup)v.Parent;
ViewGroup tempOwner = (ViewGroup)vtemp.Parent;
tempOwner.RemoveView(vtemp);
owner.RemoveView(v);
tempOwner.AddView(v);
owner.AddView(vtemp);
break;
case DragAction.Ended:
v.SetBackgroundDrawable(normalShape);
break;
default:
break;
}
return true;
}
答案 1 :(得分:0)
还有一种方法可以解决这个问题。
case DragAction.Drop:
View v1LocalState = (View)e.LocalState;//event argument-e
LinearLayout v1LinearLay = (LinearLayout)v1LocalState.Parent;
v1LinearLay.RemoveView(v1LocalState);
View current = (View)v;//view argument-v
LinearLayout curLinearLayout =(LinearLayout) current.Parent;
curLinearLayout.RemoveView(current);
v1LinearLay.AddView(current);
curLinearLayout.AddView(v1LocalState);