我有一个包含按钮的relativelayout
此按钮可添加单击时拖放的文本视图
我的问题是如何防止这些文本在彼此拖放时相互重叠
动态添加和拖动textviews我说的看起来像this
我的主要活动
theme(
strip.text.x = element_blank(),
strip.background = element_blank())
我的clicklistener
public class MainActivity extends Activity
{
RelativeLayout dropLayoutNew;
Button addText;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dropLayoutNew = (RelativeLayout) findViewById(R.id.edtxtlayout);
dropLayoutNew.setOnDragListener(new MyDragListener());
addText = (Button) findViewById(R.id.mainButton1);
addText.setOnClickListener(new MyButtonClickListener());
}
}
** MyLongClickListner **
public class MyButtonClickListener implements OnClickListener
{
@Override
public void onClick(View v)
{
ViewGroup relativeParent = (ViewGroup) v.getParent();
CustomEdittext edttxt = new CustomEdittext(v.getContext());
relativeParent.addView(edttxt);
edttxt.setOnLongClickListener(new MyLongClickListener());
}
}
我的拖拉机
public class MyLongClickListner implements OnLongClickListener
{
@Override
public boolean onLongClick(View v)
{
ClipData dragdata = ClipData.newPlainText("","");
View.DragShadowBuilder shdwbldr = new View.DragShadowBuilder(v);
v.startDrag(dragdata, shdwbldr, v, 0);
v.setVisibility(View.INVISIBLE);
return true;
}
}
myButtonClickListener
public class MyDragListener implements OnDragListener
{
private LayoutParams params;
@Override
public boolean onDrag(View v, DragEvent event)
{
View view = (View) event.getLocalState();
switch(event.getAction())
{
case DragEvent.ACTION_DRAG_STARTED:
params = (RelativeLayout.LayoutParams) view.getLayoutParams();
break;
case DragEvent.ACTION_DRAG_ENTERED:
int x = (int) event.getX();
int y = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_EXITED :
break;
case DragEvent.ACTION_DRAG_LOCATION :
x= (int) event.getX();
y = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_ENDED :
break;
case DragEvent.ACTION_DROP:
x = (int) event.getX();
y = (int) event.getY();
params.leftMargin = x;
params.topMargin = y;
view.setLayoutParams(params);
view.setVisibility(View.VISIBLE);
break;
default: break;
}
return true;
}
}
我的布局
public class MyButtonClickListener implements OnClickListener
{
@Override
public void onClick(View v)
{
ViewGroup relativeParent = (ViewGroup) v.getParent();
TextView txt = new TextView(v.getContext());
relativeParent.addView(txt);
txt.setText("Drag me");
txt.setOnLongClickListener(new MyLongClickListner());
}
}