我正在处理拖放应用程序,一切正常但我看到了一个问题。我有3个ImageViews,其中两个是可拖动的对象,另一个是放置目标。每当我将物体放在掉落目标以外的任何位置时,它就完全消失了!
以下是我使用的代码:
ImageView iv1, iv2, iv3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControls();
}
private void initControls() {
// TODO Auto-generated method stub
iv1 = (ImageView) findViewById (R.id.imageView1);
iv2 = (ImageView) findViewById (R.id.imageView2);
iv1.setTag("a");
iv2.setTag("b");
iv3 = (ImageView) findViewById (R.id.imageView3);
iv1.setOnTouchListener(new MyTouchListener());
iv2.setOnTouchListener(new MyTouchListener());
iv3.setOnDragListener(new MyDragListener());
}
private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
}
class MyDragListener implements OnDragListener {
//Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
//Drawable normalShape = getResources().getDrawable(R.drawable.shape);
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_LOCATION:
if(v != iv3)
v.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_STARTED:
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
//handle the dragged view being dropped over a drop view
View view = (View) event.getLocalState();
Drawable dropshadow = getResources().getDrawable(R.drawable.dropshadow);
//view dragged item is being dropped on
ImageView dropTarget = (ImageView) v;
//view being dragged and dropped
ImageView dropped = (ImageView) view;
/*
//if an item has already been dropped here, there will be a tag
Object tag = dropTarget.getTag();
//if there is already an item here, set it back visible in its original place
if(tag!=null)
{
//the tag is the view id already dropped here
int existingID = (Integer)tag;
//set the original view visible again
findViewById(existingID).setVisibility(View.VISIBLE);
}
//set the tag in the target view to the ID of the view being dropped
dropTarget.setTag(dropped.getId());
*/
String temp = "a";
if(temp.equals(view.getTag())){
//dropped.setBackground(dropshadow);
dropTarget.setBackground(dropshadow);
Log.d(temp, "Correct Image");
} else {
Log.d("b", "Wrong Image");
view.setVisibility(View.VISIBLE);
}
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
}
我在这里想念的是什么?真的很感激任何帮助。感谢。
答案 0 :(得分:5)
最后我自己解决了这个问题。我添加了这个方法:
private boolean dropEventNotHandled(DragEvent dragEvent) {
return !dragEvent.getResult();
}
并添加了这个:
case DragEvent.ACTION_DRAG_ENDED:
if (dropEventNotHandled(event))
view.setVisibility(View.VISIBLE);
break;
答案 1 :(得分:1)
因此,当您开始拖动 - motionEvent.getAction() == MotionEvent.ACTION_DOWN
时 - 执行view.setVisibility(View.INVISIBLE);
但是当你停止拖动时,你不会对视图做任何事情。所以我猜你的视图对象仍然是不可见的。添加类似的条件语句,以便在拖动完成时将可见性翻转回可见。
假设MotionEvent.ACTION_UP是拖动事件的结束,在我的头顶上,在onTouch方法的第一个if块之后添加以下内容
else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
view.setVisibility(View.VISIBLE);
return true
}
保留最后的“else”块。