在android中,如何在拖放后将按钮保持在上一个位置
在我的android应用程序中,我有一个gridlayout,其中有五个linearlayout。四个linearlayout分别包含一个按钮,另一个linearlayout为空。我想将一个按钮拖放到空的linearlayout,当linearlayout不为空时,在拖放后,它将采用其先前的linearlayout位置。使用此代码在android中,如何在拖放后将按钮保持在上一个位置,android,button,drag-and-drop,android-linearlayout,android-gridlayout,Android,Button,Drag And Drop,Android Linearlayout,Android Gridlayout,在我的android应用程序中,我有一个gridlayout,其中有五个linearlayout。四个linearlayout分别包含一个按钮,另一个linearlayout为空。我想将一个按钮拖放到空的linearlayout,当linearlayout不为空时,在拖放后,它将采用其先前的linearlayout位置。使用此代码 ViewGroup owner = (ViewGroup) view.getParent(); LinearLayout pastContainer =
ViewGroup owner = (ViewGroup) view.getParent();
LinearLayout pastContainer = (LinearLayout) owner;
LinearLayout newcontainer = (LinearLayout) v;
int count = newcontainer.getChildCount();
如果目标linearlayout不为空(have child),则获取要保留按钮的linearlayout。我让TargetLinearLayout的子级使用此代码检查它是否为空
ViewGroup owner = (ViewGroup) view.getParent();
LinearLayout pastContainer = (LinearLayout) owner;
LinearLayout newcontainer = (LinearLayout) v;
int count = newcontainer.getChildCount();
我可以将按钮拖放到空的linearlayout,但当我尝试将按钮拖放到带有子对象的linearlayout时,会出现错误。这是我的密码
private class MyDragListener implements OnDragListener
{
@Override
public boolean onDrag(View v, DragEvent event)
{
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
LinearLayout pastContainer = (LinearLayout) owner;
LinearLayout newcontainer = (LinearLayout) v;
int count = newcontainer.getChildCount();
if(count==0)
{
owner.removeView(view);
newcontainer.addView(view);
view.setVisibility(View.VISIBLE);
}
else
{
pastContainer.addView(view);
view.setVisibility(View.VISIBLE);
}
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
}
如果目标linearlayout不为空(有子项),如何将按钮保持为上一个linearlayout?谢谢