gpt4 book ai didi

java - 调用了错误的 onDrag 类

转载 作者:行者123 更新时间:2023-12-01 12:07:07 26 4
gpt4 key购买 nike

我正在创建一个应用程序,用户可以在其中购买建筑物,这些建筑物是也实现拖放 API 的图像按钮。目前我遇到一个问题,假设我已经购买了 2 栋建筑:colonyHutOne 和 colonyHutTwo。它们都有自己的 onDrag 类和 onDragListeners 分别设置。当任一建筑物被拖动到某处时,第二个建筑物的 onDrag 类将被调用(因为它是最后一个被购买并插入到 View 中的建筑物)。我不知道要阻止这一切。应该是,当第一个建筑物被移动时,它会调用自己的 onDrag ,以便为正确的建筑物保存建筑物的坐标,但由于它只是调用最近创建的建筑物的 onDrag 类,因此不会保存坐标正确。

这是两个图像按钮的 onDragListeners 的代码:

对于 colonyHutOne 建筑:

findViewById(R.id.topHalf).setOnDragListener(new ColonyHutOneDrag(getApplicationContext()));
findViewById(R.id.bottomHalf).setOnDragListener(new ColonyHutOneDrag(getApplicationContext()));
FrameLayout.LayoutParams param1 = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
newColonyHutFrame.addView(newColonyHutOne, param1);

对于 colonyHutTwo 建筑:

findViewById(R.id.topHalf).setOnDragListener(new ColonyHutTwoDrag(getApplicationContext()));
findViewById(R.id.bottomHalf).setOnDragListener(new ColonyHutTwoDrag(getApplicationContext()));
FrameLayout.LayoutParams param1 = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
newColonyHutFrame.addView(newColonyHutTwo, param1);

这是 onDrag 类的代码:

对于 colonyHutOne 建筑:

public class ColonyHutOneDrag implements OnDragListener
{
SharedPreferences prefs;
Context passedContext;
Database data;

public ColonyHutOneDrag(Context applicationContext)
{
passedContext = applicationContext;
}//end constructor

@Override
public boolean onDrag(View v, DragEvent event)
{
float x = 0, y = 0;
switch(event.getAction())
{
case DragEvent.ACTION_DRAG_STARTED:
//drag has started
break;

case DragEvent.ACTION_DRAG_ENTERED:
//being dragged
break;

case DragEvent.ACTION_DRAG_EXITED:
//stop drag
break;

case DragEvent.ACTION_DRAG_LOCATION:
//find drag location
break;

case DragEvent.ACTION_DROP:
if (v == v.findViewById(R.id.bottomHalf))
{
//find position where dropped
x = event.getX();
y = event.getY();

//save the coordinates that the building was dropped at
data = new Database(passedContext);
data.open();
data.colonyHutOneXEntry(x);
data.colonyHutOneYEntry(y);
data.close();
Toast.makeText(passedContext, "Going in1 -> x: " + x + " y: " + y, Toast.LENGTH_SHORT).show();

//use this to fix building loadup glitch
View view = (View) event.getLocalState();
ViewGroup group = (ViewGroup) view.getParent();
group.removeView(view);
FrameLayout contain = (FrameLayout) v;
contain.addView(view);
view.setX(x - (view.getWidth()/2));
view.setY(y - (view.getHeight()/2));
view.setVisibility(View.VISIBLE);
}//end if
else
{
View view = (View) event.getLocalState();
view.setVisibility(View.VISIBLE);
}//end else
break;

default:
break;
}//end switch
return true;
}//end onDrag function
}//end ColonyHutOneDrag class

对于 colonyHutTwo 建筑:

public class ColonyHutTwoDrag implements OnDragListener
{
SharedPreferences prefs;
Context passedContext;
Database data;

public ColonyHutTwoDrag(Context applicationContext)
{
passedContext = applicationContext;
}//end constructor

@Override
public boolean onDrag(View v, DragEvent event)
{
float x = 0, y = 0;
switch(event.getAction())
{
case DragEvent.ACTION_DRAG_STARTED:
//drag has started
break;

case DragEvent.ACTION_DRAG_ENTERED:
//being dragged
break;

case DragEvent.ACTION_DRAG_EXITED:
//stop drag
break;

case DragEvent.ACTION_DRAG_LOCATION:
//find drag location
break;

case DragEvent.ACTION_DROP:
if (v == v.findViewById(R.id.bottomHalf))
{
//find position where dropped
x = event.getX();
y = event.getY();

//save the coordinates that the building was dropped at
data = new Database(passedContext);
data.open();
data.colonyHutTwoXEntry(x);
data.colonyHutTwoYEntry(y);
data.close();
Toast.makeText(passedContext, "Going in2 -> x: " + x + " y: " + y, Toast.LENGTH_SHORT).show();

//use this to fix building loadup glitch
View view = (View) event.getLocalState();
ViewGroup group = (ViewGroup) view.getParent();
group.removeView(view);
FrameLayout contain = (FrameLayout) v;
contain.addView(view);
view.setX(x - (view.getWidth()/2));
view.setY(y - (view.getHeight()/2));
view.setVisibility(View.VISIBLE);
}//end if
else
{
View view = (View) event.getLocalState();
view.setVisibility(View.VISIBLE);
}//end else
break;

default:
break;
}//end switch
return true;
}//end onDrag function
}//end ColonyHutTwoDrag class

这是开始拖动的 onTouchListener:

public class BuildingsClick implements OnTouchListener 
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());

String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
ClipData data = new ClipData(v.getTag().toString(), mimeTypes, item);
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);

v.startDrag(data, shadowBuilder, v, 0);
v.setVisibility(View.INVISIBLE);

return false;
}//end onTouch function

}//end BuildingsClick class

即使我拖动第一个建筑物,每次都会调用第二个建筑物的 onDrag 对我来说毫无意义。请大家帮忙。

谢谢。

最佳答案

我认为您的问题是您将 findViewById(R.id.topHalf)OnDragListener 设置为 new ColonyHutOneDrag(getApplicationContext()).

然后将同一 View 的 OnDragListener 设置为 new ColonyHutTwoDrag(getApplicationContext())在那之后。第二个调用会覆盖第一个调用,并且来自 ColonyHutOneDrag 的代码永远不会运行,因为它不再是 Activity 监听器。

同样的问题会影响您的 View findViewById(R.id.bottomHalf)

您要么想要将两个监听器合并为一个,让监听器决定其建筑类型,要么显式设置每个 View 的 OnDragListener。我建议采用前一种方法。

编辑:如果您的自定义类是正在传递的 View 的子类,这将起作用。如果您的类不是 View 的子类化,您可以在其中一个类中使用变量来区分另一个类(就像您比较的字符串而不是 if(v instance of class))。

switch(((View)v.getParent()).getID())
{
case R.id.topHalf:
if(v instanceof ColonyHutOne)
topHalfColonyOne();
else if(v instanceof ColonyHutTwo)
topHalfColonyTwo();
break;
case R.id.bottomHalf:
if(v instanceof ColonyHutOne)
bottomHalfColonyOne();
else if(v instanceof ColonyHutTwo)
bottomHalfColonyTwo();
break;
}

关于java - 调用了错误的 onDrag 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27512571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com