如果我拖动countryTypical图像并放入imageCountry [n9],应用程序会在右侧countryTypical图像上设置复选标记图像。 如果我拖动正确的countryTypical图像,并且拖动错误的countryTypical图像,也会发生这种情况。我想纠正错误。 非常感谢......
String[] countryNames = {
"Italy", "France", "Spain", "Germany", "Belgium", "Portugal", "Switzerland", "Great Britain"
};
int[] countryImages = {
R.drawable.italy,
R.drawable.france,
R.drawable.spain,...
};
int[] countryFlags = {
R.drawable.italian_flag,
R.drawable.french_flag,
R.drawable.spanish_flag,....
};
在我的OnCreate中:
countryTypical1.setImageResource(countryFlags[n1]);
countryTypical2.setImageResource(countryFlags[n2]);
countryTypical3.setImageResource(countryFlags[n3]);
countryTypical4.setImageResource(countryFlags[n4]);
countryTypical5.setImageResource(countryFlags[n5]);
countryTypical6.setImageResource(countryFlags[n6]);
countryTypical7.setImageResource(countryFlags[n7]);
countryTypical8.setImageResource(countryFlags[n8]);
countryTypical1.setOnTouchListener(dragClick);
countryTypical2.setOnTouchListener(dragClick);
countryTypical3.setOnTouchListener(dragClick);
countryTypical4.setOnTouchListener(dragClick);
countryTypical5.setOnTouchListener(dragClick);
countryTypical6.setOnTouchListener(dragClick);
countryTypical7.setOnTouchListener(dragClick);
countryTypical8.setOnTouchListener(dragClick);
imageCountry.setOnDragListener(DropListener);
View.OnTouchListener dragClick = new View.OnTouchListener(){
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
DragShadow dragShadow = new DragShadow(view);
ClipData dragData = ClipData.newPlainText("", "");
view.startDrag(dragData, dragShadow, view,0);
return true;
}
return true;
}
};
private class DragShadow extends View.DragShadowBuilder{
public DragShadow(View view) {
super(view);
}
@Override
public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
View view = getView();
int height = view.getHeight();
int width = view.getWidth();
shadowSize.set(width, height);
shadowTouchPoint.set(width, height);
}
@Override
public void onDrawShadow(Canvas canvas) {
getView().draw(canvas);
}
}
View.OnDragListener DropListener = new View.OnDragListener() {
@Override
public boolean onDrag(View view, DragEvent event) {
assignDropTrue(countryTypical1, event, n1);
assignDropTrue(countryTypical2, event, n2);
assignDropTrue(countryTypical3, event, n3);
assignDropTrue(countryTypical4, event, n4);
assignDropTrue(countryTypical5, event, n5);
assignDropTrue(countryTypical6, event, n6);
assignDropTrue(countryTypical7, event, n7);
assignDropTrue(countryTypical8, event, n8);
return true;
}
};
private void assignDropTrue(ImageView countryTypical, DragEvent event, int n1){
if (n9 == n1){
int dragEvent = event.getAction();
switch (dragEvent) {
case DragEvent.ACTION_DROP:
countryTypical.setImageResource(R.drawable.vcheckmark);
}}
}
答案 0 :(得分:0)
我写了一个样本只是为了让你了解我提出的逻辑背后的想法(a.k.a它没有经过测试)
View.OnDragListener DropListener = new View.OnDragListener() {
@Override
public boolean onDrag(View view, DragEvent event) {
int action = event.getAction();
View draggedView = (View) event.getLocalState();
//view is the country where the flag was dropped, draggedView is the flag you dropped
//below we check if the correct flag is dropped in the correct country
switch (event.getAction()){
case DragEvent.ACTION_DROP:
for (int i; i< countryImages.length(); i++){
if (draggedView.getId() == countryFlags[i] && view.getId()== countryImages[i])
draggedView.setImageResource(R.drawable.vcheckmark);
}
break;
default:
break;
}
return true;
}
};