我一直在尝试在Android中实现一个非常简单的拖放操作,其中拖动的项目与放置它的项目交换位置。
我已经成功实现了onLongClick和onDrag监听器。当我拖放一个项目时,它会替换掉它所放置的项目,但我无法弄清楚如何使被替换的项目取代被拖动的项目。
我花了一些时间寻找答案,但大多数答案只是链接了涉及许多代码的拖放的复杂实现,我无法理解它们以将它们应用到我的代码中。
我最初假设我没有使用
获得ImageView的真实副本ImageView temp = (ImageView) event.getLocalState();
所以我试过
ImageView dragged = (ImageView) findViewById(temp.getId());
但这并没有什么不同。
我几乎可以肯定问题出现在'OnDragListener'中,在'case DragEvent.ACTION_DROP:'中。 这是我的代码(来自Main.java):
public class Main extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.ball1).setOnLongClickListener(listenClick);
findViewById(R.id.ball1).setOnDragListener(listenDrag);
findViewById(R.id.target1).setOnLongClickListener(listenClick);
findViewById(R.id.target1).setOnDragListener(listenDrag);
findViewById(R.id.target2).setOnDragListener(listenDrag);
findViewById(R.id.target2).setOnLongClickListener(listenClick);
findViewById(R.id.target3).setOnDragListener(listenDrag);
findViewById(R.id.target3).setOnLongClickListener(listenClick);
View.OnLongClickListener listenClick = new View.OnLongClickListener()
{
@Override
public boolean onLongClick(View v)
{
ClipData data = ClipData.newPlainText("","");
DragShadow dragShadow = new DragShadow(v);
v.startDrag(data, dragShadow, v, 0);
return false;
}
};
private class DragShadow extends View.DragShadowBuilder
{
ColorDrawable greyBox;
public DragShadow(View view)
{
super(view);
greyBox = new ColorDrawable(Color.LTGRAY);
}
@Override
public void onDrawShadow(Canvas canvas)
{
greyBox.draw(canvas);
}
@Override
public void onProvideShadowMetrics(Point shadowSize,
Point shadowTouchPoint)
{
View v = getView();
int height = (int) v.getHeight();
int width = (int) v.getWidth();
greyBox.setBounds(0, 0, width, height);
shadowSize.set(width, height);
shadowTouchPoint.set((int)width/2, (int)height/2);
}
}
View.OnDragListener listenDrag = new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event)
{
int dragEvent = event.getAction();
switch (dragEvent)
{
case DragEvent.ACTION_DRAG_ENTERED:
Log.i("Drag Event", "Entered");
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.i("Drag Event", "Exited");
break;
case DragEvent.ACTION_DROP:
ImageView target = (ImageView) v;
ImageView temp = (ImageView) event.getLocalState();
ImageView dragged = (ImageView) findViewById(temp.getId());
target.setId(dragged.getId());
target.setImageDrawable(dragged.getDrawable());
dragged.setId(temp.getId());
dragged.setImageDrawable(temp.getDrawable());
break;
}
return true;
}
};
如果有人能告诉我为什么setImageDrawable在目标上工作,而不是在拖动,我会非常感激。
答案 0 :(得分:5)
进入计算机科学的一大乐趣就是学习谦虚。至少有90%的时间,这个任务的答案是:“为什么这不起作用?”是你告诉电脑做错了。
在“case DragEvent.ACTION_DROP:”中我得到了对两个视图的引用。我出于某种原因没有意识到的是,当我做的时候
target.setImageDrawable(dragged.getDrawable());
我也在改变temp的drawable,因为temp指的是实际的视图。因为那时我做了
dragged.setImageDrawable(temp.getDrawable());
我实际上只是将拖动的drawable设置为原来的状态。 这是更正后的代码。
case DragEvent.ACTION_DROP:
ImageView target = (ImageView) v;
ImageView dragged = (ImageView) event.getLocalState();
Drawable target_draw = target.getDrawable();
Drawable dragged_draw = dragged.getDrawable();
dragged.setImageDrawable(target_draw);
target.setImageDrawable(dragged_draw);
break;
答案 1 :(得分:0)
我更新了答案,可以实际交换视图而不是交换图像;
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
private fun initView(view: View) {
view.setOnDragListener(View.OnDragListener { target, event ->
when (event.action) {
DragEvent.ACTION_DRAG_STARTED -> return@OnDragListener true
DragEvent.ACTION_DRAG_ENTERED -> {
target.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent))
return@OnDragListener true
}
DragEvent.ACTION_DRAG_LOCATION -> return@OnDragListener true
DragEvent.ACTION_DRAG_EXITED -> {
target.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary))
return@OnDragListener true
}
DragEvent.ACTION_DROP -> {
target.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary))
val dragged = event.localState as ImageView
val targetView = target as ImageView
//Swap Image
// val target_draw = targetView!!.drawable
// val dragged_draw = dragged!!.drawable
// dragged.setImageDrawable(target_draw)
// targetView.setImageDrawable(dragged_draw)
val oldOwner = dragged.parent as ViewGroup
val newOwner = target.parent as ViewGroup
val draggedPosition = oldOwner.indexOfChild(dragged)
val targetPosition = oldOwner.indexOfChild(dragged)
oldOwner.removeView(dragged)
newOwner.addView(dragged,targetPosition)
newOwner.removeView(target)
oldOwner.addView(target,draggedPosition)
return@OnDragListener true
}
DragEvent.ACTION_DRAG_ENDED -> return@OnDragListener true
else -> {
}
}
false
})
view.setOnLongClickListener { v ->
val dummyData = ClipData.newPlainText("dummyData", "") // don't forget to pass empty String
val shadowBuilder = View.DragShadowBuilder(v)
v.startDrag(dummyData, shadowBuilder, v, 0)
//val data = ClipData.newPlainText("value", "1")
// view.startDrag(data, View.DragShadowBuilder(v), null, 0)
true
}
}