我需要能够“添加一个按钮并将其拖放到某个位置”出于某种奇怪的原因,我没有从我的拖拉机中获取任何记录,我只能从左上角移动到右下角角落(!?) 添加按钮时,我会触发以下代码
@OnClick(R.id.remote_add_button)
public void addRemoteNewButton() {
fabMenu.collapse();
enableDragAndDropLayout(true);
View v = new ImageView(getActivity());
v.setTag("New button");
v.setBackgroundColor(getResources().getColor(R.color.primary_dark));
layoutParams = new RelativeLayout.LayoutParams(60, 60);
layoutParams.setMargins(50, 50, 0, 0);
remoteContainer.addView(v, layoutParams);
v.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Timber.d("onClick()");
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(v);
v.startDrag(dragData, myShadow, null, 0);
return true;
}
});
v.setOnDragListener(new MyDragListener());
}
private class MyDragListener implements View.OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
int x_cord;
int y_cord;
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
Timber.i("Action is DragEvent.ACTION_DRAG_STARTED");
break;
case DragEvent.ACTION_DRAG_ENTERED:
x_cord = (int) event.getX();
y_cord = (int) event.getY();
Timber.i("Action is DragEvent.ACTION_DRAG_ENTERED");
break;
case 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);
Timber.i("Action is DragEvent.ACTION_DRAG_EXITED");
break;
case DragEvent.ACTION_DRAG_LOCATION:
x_cord = (int) event.getX();
y_cord = (int) event.getY();
Timber.i("Action is DragEvent.ACTION_DRAG_LOCATION");
break;
case DragEvent.ACTION_DROP:
x_cord = (int) event.getX();
y_cord = (int) event.getY();
Timber.i("Action is DragEvent.ACTION_DROP");
break;
case DragEvent.ACTION_DRAG_ENDED:
x_cord = (int) event.getX();
y_cord = (int) event.getY();
Timber.i("Action is DragEvent.ACTION_DRAG_ENDED");
break;
default:
Timber.i(event.toString());
break;
}
return true;
}
}
任何人都能指出正确的方向吗?因为我似乎找不到合适的解决方案。我尝试了很多教程,编辑代码导致一些只是没有任何意义。尝试那里的“例子”,但都给出了相同的结果。 有小费吗?将是一个令人敬畏的X-mas礼物; p 干杯,享受假期吧!
答案 0 :(得分:1)
此示例可以为您提供帮助:github.com/Humoule/DragDropApplication - Farouk Touzi
感谢此评论,我能够解决它! :)
这是我解决问题的方法:
public void newButton(){
View newButton = new View(getActivity());
newButton.setTag("New button");
newButton.setBackgroundColor(getResources().getColor(R.color.primary_dark));
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(100, 100);
layoutParams.setMargins(50, 50, 0, 0);
dragAndDropLayout.addView(newButton, layoutParams);
newButton.setOnTouchListener(new MyTouchListener());
remoteContainer.setOnDragListener(new MyDragListener());
}
private final class MyDragListener implements View.OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
Timber.d("onDrag(%s, %s)", v.toString(), event.toString());
// Store the action type for the incoming event
final int action = event.getAction();
// Handles each of the expected events
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
Timber.i("ACTION_DRAG_STARTED");
// Invalidate the view to force a redraw in the new tint
v.invalidate();
// Returns true to indicate that the View can accept the
// dragged data.
return true;
case DragEvent.ACTION_DRAG_ENTERED:
Timber.e("ACTION_DRAG_ENTERED");
// Invalidate the view to force a redraw in the new tint
v.invalidate();
return true;
case DragEvent.ACTION_DRAG_LOCATION:
// Ignore the event
return true;
case DragEvent.ACTION_DRAG_EXITED:
Timber.d("ACTION_DRAG_EXITED");
// Invalidate the view to force a redraw in the new tint
v.invalidate();
return true;
case DragEvent.ACTION_DROP:
Timber.v("ACTION_DROP");
// Gets the item containing the dragged data
ClipData dragData = event.getClipData();
ViewGroup viewGroup = (ViewGroup) v;
for(int i = 0; i < viewGroup.getChildCount();i++){
View child = viewGroup.getChildAt(i);
if(child.getTag() != null && child.getTag().equals("New button")){
viewGroup.removeView(child);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(100, 100);
layoutParams.setMargins((int) event.getX(), (int) event.getY(), 0, 0);
viewGroup.addView(child, layoutParams);
child.setVisibility(View.VISIBLE);
break;
}
}
// Gets the text data from the item.
final String tag = dragData.getItemAt(0).getText().toString();
// Displays a message containing the dragged data.
Toast.makeText(getActivity(), "The dragged image is " + tag, Toast.LENGTH_SHORT).show();
// Invalidates the view to force a redraw
v.invalidate();
return true;
case DragEvent.ACTION_DRAG_ENDED:
Timber.w("ACTION_DRAG_ENDED");
// Invalidates the view to force a redraw
v.invalidate();
return true;
default:
break;
}
return false;
}
}
private final class MyTouchListener implements View.OnTouchListener {
public boolean onTouch(View v, MotionEvent motionEvent) {
String tag = v.getTag().toString();
// Instantiates the drag shadow builder
View.DragShadowBuilder mShadow;
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
ClipData data = ClipData.newPlainText("some label", tag);
mShadow = new View.DragShadowBuilder(v);
v.startDrag(data, mShadow, null, 0);
v.setVisibility(View.GONE);
break;
case MotionEvent.ACTION_UP:
v.performClick();
break;
default:
break;
}
return false;
}
}