Java 拖放(如果我拖动右侧图像,请选中)android
如果我拖动countryTypical图像并放入imageCountry[n9],应用程序会在右侧countryTypical图像上设置复选标记图像。 如果我拖动了正确的countryTypical图像,也拖动了错误的countryTypical图像,就会发生这种情况。我想纠正这个错误。 非常感谢Java 拖放(如果我拖动右侧图像,请选中)android,java,android,drag-and-drop,Java,Android,Drag And Drop,如果我拖动countryTypical图像并放入imageCountry[n9],应用程序会在右侧countryTypical图像上设置复选标记图像。 如果我拖动了正确的countryTypical图像,也拖动了错误的countryTypical图像,就会发生这种情况。我想纠正这个错误。 非常感谢 String[] countryNames = { "Italy", "France", "Spain", "Germany", "Belgium", "Portugal", "Swi
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(){
我写了一个示例,只是想让您了解我提出的逻辑背后的想法(也就是说,它没有经过测试)
View.OnDragListener-DropListener=newview.OnDragListener(){
@凌驾
公共布尔onDrag(视图,DrageEvent事件){
int action=event.getAction();
View draggedView=(View)event.getLocalState();
//view是国旗降下的国家,draggedView是您降下的国旗
//下面我们检查是否在正确的国家降下了正确的国旗
开关(event.getAction()){
案例DrageEvent.ACTION_DROP:
对于(int i;i
'if(n9==n1)”,你的例子中的n9是什么?n9=r.nextInt(countryFlags.length);问题是在switch的例子中,如果你正在删除具有正确映射的图像,你可能必须检查DragEvent.ACTION\u DROP的例子。我不能再帮你了,因为我不太了解你代码的所有方面。(例如,n1-n8是什么?0-7之间的整数?)完全不同。因此图像都不同。我希望只有在拖放正确的图像时,它才会成为选中标记。
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);
}}
}
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;
}
};