在我的应用程序中,我有 10 个线性布局用作 ImageView 的拖放区。我想将 ImageView(纸牌游戏)拖到其放置区。这部分工作正常。问题是,我想让用户只做一个动作 - 然后取消 DragListener 以便 ImageView 将被卡在其放置区域。有任何想法吗?这是我的代码中的一些内容:
private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
SetDragListner();
if (TouchTheStart != false) {
if (CountTheTime < 20) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
CountCards();
if(ReachedDestination==false){
Random = getRandom();
getCard();
}
ReachedDestination=true;
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
view);
view.startDrag(data, shadowBuilder, view, 0);
return true;
} else {
return false;
}
}
}
return true;
}
}
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) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackground(enterShape);
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackground(normalShape);
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
container.addView(view);
Cards.remove(Random);
CountTheTime++;
ReachedDestination=false;
break;
case DragEvent.ACTION_DRAG_ENDED:
v.setBackground(normalShape);
default:
break;
}
return true;
}
}