问题描述
嘿朋友帮我实现目标。 我是android开发的新手,我唯一想做的就是我要拖动一个按钮并将其拖放到另一个View(即RelativeLayout)上,这是我完成拖动代码的第一件事。
rootView = inflater.inflate(R.layout.fragment_game_map,container,false);
button1 = (Button) rootView.findViewById(R.id.button1);
button1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(),mimeTypes, item);
View.DragShadowBuilder myShadow = new View.DragShadowBuilder(button1);
v.startDrag(dragData,myShadow,null,0);
return true;
}
});
button1.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_STARTED");
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_ENTERED");
int x_cord = (int) event.getX();
int y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_EXITED");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
layoutParams.leftMargin = x_cord;
layoutParams.topMargin = y_cord;
v.setLayoutParams(layoutParams);
break;
case DragEvent.ACTION_DRAG_LOCATION:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_LOCATION");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(TAG, "Action is DragEvent.ACTION_DRAG_ENDED");
// Do nothing
break;
case DragEvent.ACTION_DROP:
Log.d(TAG, "ACTION_DROP event");
// Do nothing
break;
default:
break;
}
return true;
}
});
button1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(button1);
button1.startDrag(data, shadowBuilder, button1, 0);
button1.setVisibility(View.INVISIBLE);
return true;
}
else
{
return false;
}
}
});
return rootView;
}
但是第二件事我没有想法可以做..那就是我想把它放到RelativeLayout上,但是RelativeLayout只能允许一个按钮放到它上面。
1楼
您错过了DragLiatenr的要点。 DragListenr侦听拖动事件,并正确处理它们,因此,如果您的RelativeLayout未注册到DragEvent,他将无法处理它们。
实际上,对于您的要求,您只需要侦听ACTION_DROP,并为所有其他事件返回true。
您可能会喜欢这样的东西:
final RelativeLayout container; //You need to initialize your Relativelayout here
container.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DROP:
Log.d(TAG, "ACTION_DROP event");
((ViewGroup)v.getParent()).removeView(v); //you must first remove view from it former container
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
container.addView(v, params); // of course you need to modify params for your need
return true;
break;
default:
return true;
}
}
});
另外,为什么还要放置从未使用过的onTouchListenr?